开始使用 Perl 6 单行脚本

Perl与其他语言的区别之一是能够用一行代码编写小程序,称为“单行脚本”。直接在终端中输入程序通常比编写临时脚本更快。而且单行脚本也很强大;它们是完整的Perl程序,可以加载外部库,还可以集成到终端中。您可以将数据输入或输出到单行脚本中。

与前辈一样,Perl 6支持单行脚本。同样,Perl 6在清理Perl 5的其他缺陷时,单行脚本的语法也得到了改进。它更简洁,有更少的特殊变量和选项需要记忆。本文旨在帮助您开始使用Perl 6单行脚本。

基础知识

要开始使用单行脚本,您只需真正理解-e选项即可。这告诉Perl执行后面的内容作为一个程序。例如

perl6 -e 'say "Hello, World!"'

让我们逐步分析这段代码。

  1. perl6调用Perl 6程序
  2. -e告诉Perl 6执行
  3. 'say "Hello, World!"'是程序。每个程序都必须用单引号括起来(在Windows上除外,见(转换为Windows))。

要运行单行脚本,只需将其输入到终端

> perl6 -e 'say "Hello, World!"'
Hello, World!

文件处理

如果您想加载文件,只需在程序代码后添加文件的路径即可

> perl6 -e 'for (lines) { say $_ }' /path/to/file.txt

此程序打印出/path/to/file.txt中的每一行。您可能知道$_是默认变量,在这种情况下是正在循环的当前行。lines是一个列表,每当您将文件路径传递给单行脚本时,系统会自动为您创建它。现在让我们一步一步地重写这个单行脚本。这些单行脚本都是等效的

> perl6 -e 'for (lines) { say $_ }' /path/to/file.txt
> perl6 -e 'for (lines) { $_.say }' /path/to/file.txt
> perl6 -e 'for (lines) { .say }' /path/to/file.txt
> perl6 -e '.say for (lines)' /path/to/file.txt
> perl6 -e '.say for lines' /path/to/file.txt

就像$_是默认变量一样,在默认变量上调用的方法可以省略变量名。它们成为默认方法。因此$_.say变成了.say。这种简洁性在单行脚本中得到了回报——它减少了打字量!

-n选项会改变程序的行为:它对文件的每一行执行一次代码。要将/path/to/file.txt的每一行转换为大写并打印出来,您可以输入

> perl6 -ne '.uc.say' /path/to/file.txt

-p选项与-n类似,但会自动打印$_。这意味着我们还可以通过以下方式将文件转换为大写

> perl6 -pe '$_ = $_.uc' /path/to/file.txt

或者通过应用快捷方式,这会产生相同的效果

> perl6 -pe '.=uc' /path/to/file.txt

-n-p选项非常有用,通常可以省去程序员额外的打字。

加载模块

您应该知道的最后一件事情是如何加载模块。这非常强大,因为您可以通过导入外部库来扩展Perl 6的功能。-M开关代表加载模块

> perl6 -M URI::Encode -e 'say encode_uri("/10 ways to crush it with Perl 6")'

代码-M URI::Encode加载了URI::Encode模块,该模块导出了encode_uri子程序。它打印

%2F10%20ways%20to%20crush%20it%20with%20Perl%206

如果您有一个未安装在标准位置的模块怎么办?在这种情况下,仅使用-M不会起作用,因为Perl找不到该模块。对于这些场景,只需传递使用-I开关以包含目录

> perl6 -I lib -M URI::Encode -e 'say encode_uri("www.example.com/10 ways to crush it with Perl 6")'

现在Perl 6将在lib以及标准安装位置中搜索URI::Encode。

最后,如果您想查看所有这些选项的摘要,只需使用-h选项

> perl6 -h

这将打印

    With no arguments, enters a REPL. With a "[programfile]" or the "-e" option, compiles the given program and by default also executes the compiled code.
 
    -c                   check syntax only (runs BEGIN and CHECK blocks)
    --doc                extract documentation and print it as text
    -e program           one line of program
    -h, --help           display this help text
    -n                   run program once for each line of input
    -p                   same as -n, but also print $_ at the end of lines
    -I path              adds the path to the module search path
    -M module            loads the module prior to running the program
    --target=[stage]     specify compilation stage to emit
    --optimize=[level]   use the given level of optimization (0..3)
    -t, --trace=[flags]  enable trace flags, see 'parrot --help-debug'
    --encoding=[mode]    specify string encoding mode
    -o, --output=[name]  specify name of output file
    -v, --version        display version information
    --stagestats         display time spent in the compilation stages
    --ll-exception       display a low level backtrace on errors
    --profile            print profile information to standard error
    --doc=[module]       Use Pod::To::[module] to render inline documentation.
     
    Note that only boolean single-letter options may be bundled.

    Output from --profile can be visualized by kcachegrind.

    To modify the include path, you can set the PERL6LIB environment variable:

    PERL6LIB="lib" perl6 example.pl

    For more information, see the perl6(1) man page.

结论

本文改编自我的开源书籍,其中包含大量Perl 6单行示例,许多示例都是由Perl 6社区贡献的。如果您想了解更多Perl 6,我建议访问官方网站,该网站提供了IRC频道和官方文档的链接。


本文最初发布在PerlTricks.com

标签

David Farrell

David是一位专业程序员,他经常在推特博客上分享关于代码和编程艺术的见解。

浏览他们的文章

反馈

本文有错误吗?请在GitHub上打开一个issue或pull request来帮助我们。