Perlcritic - error with two arguments "open"

I have a script and am trying to identify bad practices using perlcritic.

One line I have is the following:

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

This gives this error: Two "open" arguments used in row xxx, column x. See page 207 PBP. (Severity level: 5)

Any ideas on how to fix it?

+4
source share
2 answers

To make Perl shut up critically, but do nothing good, just change the code to:

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

Please note, however, that this is absolutely not better in any way from the much more obvious:

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

Because you do not share your tokens for calling exec directly. It would look like this:

 open(my $PIPE_FROM_FREESPCHK, "-|", $cmd_name, @cmd_args) || zdie($MSG_PASSTHRU, "Error checking free space of file system."); 

The question is whether you are running a shell command or just doing something. If your free check is something like df . 2>/dev/null | awk .... df . 2>/dev/null | awk .... df . 2>/dev/null | awk .... you need a complete shell. If it is just df , then you do not.

+1
source

If you use the --verbose 11 flag, you will get a much more detailed explanation of the error. In this case, the error you get is as follows:

The two "open" arguments used on line 6 are next to 'open FILE,' somefile ';'.
InputOutput :: ProhibitTwoArgOpen (Severity Level: 5)

A form with three arguments to `open '(introduced in Perl 5.6) prevents subtle errors that occur when a file name begins with funny characters such as'>' or '<'. The IO :: File module provides a nice object-oriented interface with file descriptors, which I think is more elegant.

  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! 

Also, the file entry mode is determined more explicitly, since the difference between the two is:

  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 perl versions prior to 5.6 via the include statement, for example using `require 5.005 'in it.

I found this by reading the perlcritic documentation.

+22
source

Source: https://habr.com/ru/post/1387023/


All Articles