Perl标志-pe,-pi,-p,-w,-d,-i,-t?

Tud*_*tin 102 perl flags command-line

我已经看到了许多使用不同标志运行Perl代码或脚本的方法.但是,当我尝试google每个标志的含义时,我主要将结果发送到通用Perl网站,并且没有关于标志或其使用的具体信息.

以下是我经常遇到的旗帜,我不知道它们的含义:

  • perl -pe
  • perl -pi
  • perl -p
  • perl -w
  • perl -d
  • perl -i
  • perl -t

如果你告诉我每个人的意思和一些用例,或者至少告诉我找出其含义的方法,我将非常感激.

pax*_*blo 141

是的,谷歌是用于查找标点符号非常困难的,不幸的是,Perl中似乎主要是由标点符号的:-)

命令行开关都在perlrun中详细说明.(可通过调用从命令行获得perldoc perlrun)

简要介绍一下选项,一个接一个:

-p: Places a printing loop around your command so that it acts on each
    line of standard input. Used mostly so Perl can beat the
    pants off awk in terms of power AND simplicity :-)
-n: Places a non-printing loop around your command.
-e: Allows you to provide the program as an argument rather
    than in a file. You don't want to have to create a script
    file for every little Perl one-liner.
-i: Modifies your input file in-place (making a backup of the
    original). Handy to modify files without the {copy,
    delete-original, rename} process.
-w: Activates some warnings. Any good Perl coder will use this.
-d: Runs under the Perl debugger. For debugging your Perl code,
    obviously.
-t: Treats certain "tainted" (dubious) code as warnings (proper
    taint mode will error on this dubious code). Used to beef
    up Perl security, especially when running code for other
    users, such as setuid scripts or web stuff.

  • 通常避免使用`-w`,但应在自己的代码中用`use warnings`替换它. (9认同)
  • @duskwuff:总的来说,我同意,我在自己的代码中"使用警告",但是-w确实有用 - 它有助于清除写得不好的CPAN模块.:-) (6认同)
  • 实际上,通常应该避免使用`-w`,因为它可以为所有**代码启用警告,包括未记录警告的CPAN模块.结果通常很嘈杂,也很无用. (4认同)
  • @IanBytchek可能/必须采用其他参数的参数不能在压缩列表内。-i扩展了备份。`-e`需要一个perl命令。在`-0ep`中,您告诉perl'p'是perl命令而不是参数。那根本行不通。 (2认同)

zel*_*lio 10

-p标志基本上运行脚本

while (<>) {
# exec here
}
continue {
    print or die "-p destination: $!\n";
}
Run Code Online (Sandbox Code Playgroud)

-e 允许您传递脚本 STDIN

perl -e '$x = "Hello world!\n"; print $x;'
Run Code Online (Sandbox Code Playgroud)

-i指示解释器STDIN执行脚本传递给的所有数据都将在原地完成.

-w是相同的use warnings;,但是在全球而不是局部范围内

-d 运行Perl调试器

  • `-w`与`use warnings`不完全相同,后者的范围是本地文件 (2认同)
  • 将脚本作为参数传递与在STDIN上传递它不同.-i从参数列表中获取文件名,而不是stdin.虽然STDIN通常与控制终端相关联,并且继承自读取stdin的shell并将参数列表设置为perl,但它们不是同一个东西. (2认同)

小智 8

其他人提到了perlrun.如果你使用B :: Deparse,你可以看到它意味着什么(对于大多数事情):

$ perl -MO=Deparse   -p  -e 1
LINE: while (defined($_ = <ARGV>)) {
    '???';
}
continue {
    die "-p destination: $!\n" unless print $_;
}
-e syntax OK
Run Code Online (Sandbox Code Playgroud)

1由'???'表示,因为它被优化掉了.

$ perl -MO=Deparse   -p -i  -e 1
BEGIN { $^I = ""; }
LINE: while (defined($_ = <ARGV>)) {
    '???';
}
continue {
    die "-p destination: $!\n" unless print $_;
}
-e syntax OK
Run Code Online (Sandbox Code Playgroud)

-i设置$ ^ I,就像

$ perl -MO=Deparse   -p -i.bak  -e 1
BEGIN { $^I = ".bak"; }
LINE: while (defined($_ = <ARGV>)) {
    '???';
}
continue {
    die "-p destination: $!\n" unless print $_;
}
-e syntax OK
Run Code Online (Sandbox Code Playgroud)

但请记住,<ARGV>使用2参数打开,因此没有以> </或开头/结尾的文件名|.


rus*_*tyx 5

还有一个重要的标志-n没有在列表中提到。

-n与 相同-p,只是$_默认情况下不打印。这在过滤文本文件时非常有用。

通过这种方式,Perl 可以grep | sed在单个单行中替换。

例如:

perl -ne 'print "$1\n" if /Messages read: (\d+)/' <my_input.txt

将打印出在“消息读取:”之后找到的每个整数值,仅此而已。