Perl6 REPL 使用 [英] Perl6 REPL usage

查看:39
本文介绍了Perl6 REPL 使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将您放入 REPL 之前,是否可以让 (Rakudo) Perl6 执行一些代码?就像 python 对 "python -i " 所做的一样.

Is it possible to have (Rakudo) Perl6 execute some code before dropping you into the REPL? Like python does with "python -i ".

例如,我想加载一些模块,并且可能读取一个侧文件并从该侧文件构建一些数据结构,然后再放入 REPL 并让用户在数据结构上做他们需要做的事情,使用REPL 作为用户界面.

For instance, I want to load up some modules and maybe read a side file and build some data structures from that side file before dropping into the REPL and letting the user do the things they need to do on the data structure, using the REPL as a user interface.

这与使用从文件加载的定义启动 REPL 类似但不同,尽管此问题的答案可能会满足该问题.基本情况是,在任何程序执行结束时,解释器不会退出,而是将用户留在 REPL 处.除了为交互式程序提供漂亮的、内置的、基于 Perl6 的用户界面之外,它还提供了一个很好的工具来调试代码,否则会因错误而退出.

This is similar but different than Start REPL with definitions loaded from file though answers to this question might satisfy that one. The basic case is that, at the end of execution of any program, instead of exiting, the interpreter leaves the user at the REPL. Aside from providing a nifty, built-in, Perl6-based user interface for interactive programs, it also provides a good tool from which to debug code that otherwise exits with an error.

选择 Zoffix 的解决方案作为正确的(到目前为止)解决方案,因为它是唯一满足所述所有要求的解决方案.希望将此功能添加到编译器或语言规范中.

Selecting Zoffix's solution as the correct (so far) one as it is the only one that satisfies all requirements as stated. Here's hoping this capability gets added to the compiler or language spec.

推荐答案

我想提供 Zoffix 在 IRC 上给出的答案.它满足基本要求,但远非漂亮,它使用没有用户支持的 NQP,也不能保证未来的 NQP API(nqp::*"调用)并且可以在没有警告的情况下更改.

I'd like to provide an answer that Zoffix gave on IRC. It satisfies the basic requirement but is far from pretty and it uses NQP for which there is no user support nor is the NQP API ( "nqp::*" calls ) guaranteed for the future and can change without warning.

replify 「
  say 'Hello to your custom REPL! Type `say $a` to print the secret variable';
  my $a = "The value is {rand}";
」;

sub replify (Str:D \pre-code = '') {
    use nqp;
    my %adverbs; # command line args like --MFoo
    my \r := REPL.new: nqp::getcomp('perl6'), %adverbs;
    my \enc := %adverbs<encoding>:v.Str;
    enc && enc ne 'fixed_8' && $*IN.set-encoding: enc;

    my $*CTXSAVE := r;
    my $*MAIN_CTX;
    pre-code and r.repl-eval: pre-code, $, :outer_ctx(nqp::getattr(r, REPL, '$!save_ctx')),
      |%adverbs;
    $*MAIN_CTX and nqp::bindattr(r, REPL, '$!save_ctx', $*MAIN_CTX);

    r.repl-loop: :interactive, |%adverbs;
}

这篇关于Perl6 REPL 使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆