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


当前回答

我已经创建了perli,一个运行在Linux、macOS和Windows上的Perl REPL。

它的重点是自动打印结果,方便的文档查找,并且简单 检查正则表达式匹配。 你可以在这里看到截图。

它可以独立工作(除了Perl本身,没有其他依赖项),但是强烈建议安装rlwrap,以便支持命令行编辑、持久的命令历史记录和制表符补全——在这里阅读更多信息。

安装

If you happen to have Node.js installed: npm install -g perli Otherwise: Unix-like platforms: Download this script as perli to a folder in your system's path and make it executable with chmod +x. Windows: Download the this script as perli.pl (note the .pl extension) to a folder in your system's path. If you don't mind invoking Perli as perli.pl, you're all set. Otherwise, create a batch file named perli.cmd in the same folder with the following content: @%~dpn.pl %*; this enables invocation as just perli.

其他回答

我写了一个脚本,我称之为“psh”:

#! /usr/bin/perl

while (<>) {
  chomp;
  my $result = eval;
  print "$_ = $result\n";
}

不管你输入什么,它都会在Perl中计算:

> gmtime(2**30)
gmtime(2**30) = Sat Jan 10 13:37:04 2004

> $x = 'foo'
$x = 'foo' = foo

> $x =~ s/o/a/g
$x =~ s/o/a/g = 2

> $x
$x = faa

我总是这样做:

rlwrap perl -wlne'eval;print$@if$@'

使用5.10,我已经切换到:

rlwrap perl -wnE'say eval()//$@'

(rlwrap是可选的)

Perl -d是你的朋友:

% perl -de 0

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

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

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

   perl -d -e 1