2011-12-19 9 views
4

私はスクリプトを持っていますが、私はperlcriticを使用して悪い習慣をelimetしようとしています。次のように私が持っているPerlcritic - 2つの引数 "open"エラー

1行がある:

open(my($FREESPCHK), $cmdline) || &zdie($MSG_PASSTHRU,"Error checking free space of file system."); 

これは、このエラーを与える:行xxx、x列で使用 二引数「オープン」。 PBPの207ページを参照してください。 (重大度:5)

問題を解決する方法はありますか?

+2

'$ cmdline'には何がありますか?それはファイルなのですか? – Mat

+0

'## nocritic'⁠は、これとネガティブなネタのナボブの残りの部分をすべて修正する必要があります。 – tchrist

答えて

1

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."); 

質問はあなたがシェルコマンドを実行しているか、単に何かをexec'ingされているかどうかです。あなたの無料のチェックがdf . 2>/dev/null | awk ....のようなものであれば、完全なシェルが必要です。それがちょうどdfであれば、あなたはしません。

+0

答えは本質的に正しいですが、コマンドを開く場合は、読み込み可能なファイルハンドル(コマンドの標準出力を読みたい)や '' - ''の '' - ''書き込み可能なファイルハンドル(コマンドのstdinに書きたい)。 – vstm

+3

それ以上のことはありません。コマンドが ">"で始まっていると、あなたのいわゆる同等の機能は動作しません(私はかなり一般的です)。それが批判の全体のポイントなので、そのポイントを逃してください。実際に質問に答えるために+1 – ikegami

22

フラグ--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ドキュメントを読んでこれを見つけました。

+3

+1。 –

+1

私は、 '%P'を含むカスタム' --verbose FORMAT'を使いたいと思います。これは私に、完全なポリシー名を示しています。これは、私のコマンドラインで簡単にcopy'n'pasteを使って、 'perldoc'を使ってエラーの完全な説明を簡単に得ることができます:' perldoc Perl :: Critic :: Policy :: InputOutput :: ProhibitTwoArgOpen' – toolic

関連する問題