我有一個腳本,我試圖使用perlcritic不良做法。Perlcritic - 兩個參數「打開」的錯誤
一號線我有如下:
open(my($FREESPCHK), $cmdline) || &zdie($MSG_PASSTHRU,"Error checking free space of file system.");
這給出了這樣的錯誤: 兩個參數的「開放式」的線XXX,x列使用。請參閱PBP的第207頁。 (嚴重性:5)
有關如何解決它的任何想法?
我有一個腳本,我試圖使用perlcritic不良做法。Perlcritic - 兩個參數「打開」的錯誤
一號線我有如下:
open(my($FREESPCHK), $cmdline) || &zdie($MSG_PASSTHRU,"Error checking free space of file system.");
這給出了這樣的錯誤: 兩個參數的「開放式」的線XXX,x列使用。請參閱PBP的第207頁。 (嚴重性:5)
有關如何解決它的任何想法?
爲了使Perl的評論家閉嘴,但確實沒有真正的好處都沒有,只是修改代碼:然而
open(my $PIPE_FROM_FREESPCHK, "-|", $cmdline)
|| zdie($MSG_PASSTHRU, "Error checking free space of file system.");
注意,這是絕對沒有更好的在任何距離遠些的任何方面顯而易見:
open(my $PIPE_FROM_FREESPCHK, "$cmdline |")
|| zdie($MSG_PASSTHRU, "Error checking free space of file system.");
因爲您沒有分離出用於直接調用exec
的令牌。這看起來更像是這樣的:
open(my $PIPE_FROM_FREESPCHK, "-|", $cmd_name, @cmd_args)
|| zdie($MSG_PASSTHRU, "Error checking free space of file system.");
問題是,你是否正在運行一個shell命令或只是exec'ing的東西。如果您的免費支票是df . 2>/dev/null | awk ....
,那麼您需要完整的外殼。如果它只是df
,那麼你不這樣做。
如果您使用--verbose 11
標誌,您會得到更詳細的錯誤說明。在這種情況下,你得到的是錯誤看起來是這樣的:
Two-argument "open" used at line 6, near 'open FILE, 'somefile';'.
InputOutput::ProhibitTwoArgOpen (Severity: 5)The three-argument form of `open' (introduced in Perl 5.6) prevents subtle bugs that occur when the filename starts with funny characters like '>' or '<'. The IO::File module provides a nice object-oriented interface to filehandles, which I think is more elegant anyway.
open($fh, '>output.txt'); # not ok open($fh, q{>}, 'output.txt'); # ok use IO::File; my $fh = IO::File->new('output.txt', q{>}); # even better!
It's also more explicitly clear to define the input mode of the file, as in the difference between these two:
open($fh, 'foo.txt'); # BAD: Reader must think what default mode is open($fh, '<', 'foo.txt'); # GOOD: Reader can see open mode
This policy will not complain if the file explicitly states that it is compatible with a version of perl prior to 5.6 via an include statement, e.g. by having `require 5.005' in it.
我發現這個通過閱讀perlcritic文檔。
+1,用於實際回答問題。 –
我喜歡使用包含'%P'的自定義'--verbose FORMAT'。這顯示了我的整個策略名稱,這使我可以通過'perldoc'方便地獲得錯誤的完整解釋,通過我的命令行快速copy'n'paste:'perldoc Perl :: Critic :: Policy :: InputOutput :: ProhibitTwoArgOpen' – toolic
準確地說'$ cmdline'是什麼?它是一個文件或其他東西? – Mat
'##沒有評論家'是你需要解決這個問題和所有其他的natub否定性nabobs。 – tchrist