perlperl-criticipcopen3

perl IPC:Open3 minimal to pass perlcritic?


I am reading the perlcritic documentation to avoid backticks and use IPC::Open3 here:

http://perl-critic.stacka.to/pod/Perl/Critic/Policy/InputOutput/ProhibitBacktickOperators.html

I am trying to find the least verbose option that will work and satisfy perlcritic:

#!/usr/bin/perl
use strict;
use warnings;
use IPC::Open3 'open3'; $SIG{CHLD} = 'IGNORE';
my $cmd = 'ls';
my ($w,$r,$e); open3($w,$r,$e,$cmd);
my @o = <$r>; my @e = <$e>;
1;

But it complains with the following error:

Use of uninitialized value in <HANDLE> at ipc_open3.pl line 7

Any ideas?

EDITED: Ok, here is what I've got. Unless there is a way to simplify it, I'll stick to this:

#!/usr/bin/perl
use strict;
use warnings;
use IPC::Open3 'open3'; $SIG{CHLD} = 'IGNORE';
use Symbol 'gensym';
my $cmd = 'ls';
my ($w,$r,$e) = (undef,undef,gensym); my $p = open3($w,$r,$e,$cmd);
my @o = <$r>; my @e = <$e>;
1;

Solution

  • The error parameter to IPC::Open3::open3 should not be undefined. The synopsis for IPC::Open3 uses the Symbol::gensym function to pre-initialize the error argument:

    my($wtr, $rdr, $err);
    use Symbol 'gensym';
    $err = gensym;
    $pid = open3($wtr, $rdr, $err, 'some cmd and args', 'optarg', ...);
    

    The input and output parameters can be replaced with autogenerated filehandles, so it is OK to pass undef for those arguments.

    Of course the least verbose option to satisfy perlcritic here is

    my @o = `ls 2>/dev/null`   ## no critic