2011-06-10 129 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

相關問題