如何启动Perl的交互式控制台,类似于Ruby的irb命令或python的python命令?


当前回答

Sepia和PDE也有自己的reps(用于GNU Emacs)。

其他回答

Perl没有控制台,但是调试器可以作为控制台使用。在命令提示符下,键入perl -de 1。(值“1”无关紧要,它只是一个没有任何作用的有效语句。)

Perl shell也有几个选项: 存档的“perlfaq3”页面,其中包含问题“有Perl Shell吗?”

有关更多信息,请阅读perlfaq3(当前版本)。

更新:我已经创建了一个可下载的REPL -见我的另一个答案。

事后看来:

The third-party solutions mentioned among the existing answers are either cumbersome to install and/or do not work without non-trivial, non-obvious additional steps - some solutions appear to be at least half-abandoned. A usable REPL needs the readline library for command-line-editing keyboard support and history support - ensuring this is a trouble spot for many third-party solutions. If you install CLI rlwrap, which provides readline support to any command, you can combine it with a simple Perl command to create a usable REPL, and thus make do without third-party REPL solutions. On OSX, you can install rlwrap via Homebrew with brew install rlwrap. Linux distros should offer rlwrap via their respective package managers; e.g., on Ubuntu, use sudo apt-get install rlwrap. See Ján Sáreník's answer for said combination of rlwrap and a Perl command.


你不明白Ján的答案:

自动完成 能够输入多行语句

唯一的第三方解决方案提供这些(不简单的安装+额外的,不明显的步骤)是psh,但是:

它已经有大约两年半没有活动了 它的重点是不同的,它的目标是成为一个成熟的shell替代品,因此像传统的shell一样工作,这意味着它不会像Perl语句一样自动计算命令,而需要显式的输出命令(如print)来打印表达式的结果。


Ján Sáreník的答案可以从一个方面得到改进:

默认情况下,它将数组/列表/哈希表打印为标量,也就是说,只打印它们的元素计数,而相反,枚举它们的元素会更方便。

如果您使用[sudo] cpan Data::Printer作为一次性操作安装Data::Printer模块,您可以将其加载到REPL中,以使用p()函数,您可以将列表/数组/哈希表传递给该函数进行枚举。

下面是一个名为iperl的别名,它支持readline和Data::Printer,你可以把它放在类似posix的shell初始化文件中(例如,~/.bashrc):

alias iperl='rlwrap -A -S "iperl> " perl -MData::Printer -wnE '\''BEGIN { say "# Use `p @<arrayOrList>` or `p %<hashTable>` to print arrays/lists/hashtables; e.g.: `p %ENV`"; } say eval()//$@'\'

例如,您可以执行以下操作,通过哈希表%ENV打印所有环境变量:

$ iperl        # start the REPL
iperl> p %ENV  # print key-value pairs in hashtable %ENV

与Ján的答案一样,表达式的标量结果会自动打印出来;例如:

iperl> 22 / 7  # automatically print scalar result of expression: 3.14285714285714

您可以直接进入内置调试器并从那里运行命令。

   perl -d -e 1

我想你是在问perl的REPL(读取,计算,打印,循环)接口。有几种方法可以做到这一点:

马特·特劳特(Matt Trout)有一篇文章描述了如何写一个 阿德里亚诺·费雷拉描述了一些选择 最后,你可以在irc.perl.org上登陆IRC,并在许多流行的频道中尝试其中一个评估机器人。他们会计算你传递给他们的perl代码块。

你可以在emacs中使用org-babel;打开一个组织模式文件,即tmp.org,然后你可以这样做:

#+begin_src perl :results output
@a = (1,5,9);
print ((join ", ", @a) . "\n");
$b = scalar @a;
print "$#a, $b\n";
print "$#a, " . @a . "\n";
print join ", ", 1..$#a; print "\n";
print join ", ", @a[0..$#a]
#+end_src

按CTRL-c CTRL-c将计算该块:

#+RESULTS:
#+begin_example
1, 5, 9
2, 3
2, 3
1, 2
1, 5, 9
#+end_example

我不确定这需要什么emacs配置,但我认为您可以安装https://github.com/hlissner/doom-emacs并启用它的perl和org-mode模块。