2011-06-10 73 views
81

我见过很多使用不同标志来运行Perl代码或脚本的方法。然而,当我尝试谷歌每个国旗的含义时,我主要得到的结果通用Perl网站,并没有关于标志或他们的使用的具体信息在那里找到。Perl标志-pe,-pi,-p,-w,-d,-i,-t?

下面是我遇到的最常见的标志,和我没有线索,他们的意思:

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

我会很感激,如果你能告诉我每个那些平均和一些使用情况对他们来说,或至少告诉我一个找出他们意思的方法。

+18

google搜索基本答案有关Perl往往会导致你一些非常无益的网站。总是首先检查perl自己的文档。 – ysth 2011-06-10 05:09:06

+2

借调。在这种情况下,'perldoc perlrun'列出了Perl接受的所有命令行选项。 – 2011-06-13 00:28:36

+0

[perl.com上的一些Perl命令行选项的有用指南](http://www.perl.com/pub/2004/08/09/commandline.html)。 – 2011-06-10 09:50:30

回答

113

是的,谷歌是用于查找标点符号非常困难的,不幸的是,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. 
+0

我没有注意到你提到'perldoc perlrun'。我删除了我的答案。 :-) – 2011-06-10 04:48:55

+0

谢谢您的启发回应 – 2011-06-10 04:56:12

+4

'-w'通常是可以避免的,实际上,因为它启用** all **代码的警告,包括CPAN模块,这些模块没有考虑到警告。结果通常很嘈杂,以及相当无用。 – duskwuff 2011-06-10 05:28:07

9

-p标志基本上运行与

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

-e允许脚本您将脚本传递给STDIN

perl -e '$x = "Hello world!\n"; print $x;' 

-i指示解释器执行脚本传递给STDIN的所有数据将在现场完成。

-w相同use warnings;,但在一个全球性的,而不是本地范围

-d运行Perl调试

+0

谢谢你的回答 – 2011-06-10 04:56:43

+2

'-w'与'use warnings'不太一样,后者的范围是本地文件 – plusplus 2011-06-10 09:07:05

+0

plusplus,true,补丁答案。 – zellio 2011-06-10 13:22:54

6

其他提到perlrun。如果您使用的B :: Deparse,你可以看到意思(对于大多数的东西):

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

1被表示为“???”,因为它被优化掉。

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

-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 

但要记住,<ARGV>使用2个参数的开放,所以没有与> <启动或开始| /结束的文件名。

3

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

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

通过这种方式,Perl可以用一行代码替换grep | sed

例如:

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

将打印出来后,发现每一个整数 “消息写着:”,仅此而已。

+0

这可以进一步简化。不需要“打印”$ 1 \ n“”。您可以使用“打印”来代替。 – 2017-05-09 03:10:28

+0

不,它不能,'print $ 1'与'print'('print $ _')不一样。 – rustyx 2017-05-09 18:20:38

+0

它可以:'echo abc | perl -nw -e“print if(1)”'将打印'abc'。不需要'$ 1'引用。 – 2017-05-10 12:42:16