razzz has asked for the wisdom of the Perl Monks concerning the following question:

if I send an "echo $?" command in unix via perl e.g. using the System command, how can I check the output and verify it using perl? i.e. capture the command line output and check to make sure it is correct e.g. output is a 1 or a 0. Cheers, Razzz
  • Comment on Checking and verifying a unix command sent in Perl

Replies are listed 'Best First'.
Re: Checking and verifying a unix command sent in Perl
by ikegami (Patriarch) on Jul 19, 2009 at 05:47 UTC

    system already provides the child's exit code as its return value.

    $ true $ echo $? 0 $ perl -le'print system("true") >> 8' 0 $ false $ echo $? 1 $ perl -le'print system("false") >> 8' 1

    Echoing it and capturing it is a waste.

Re: Checking and verifying a unix command sent in Perl
by jrsimmon (Hermit) on Jul 19, 2009 at 04:34 UTC
    The backtick operator `$command`; captures STDOUT from the $command issued. It does not capture STDERR (though you can tell the shell to send both STDOUT, if you want it). See perlop for the details.
Re: Checking and verifying a unix command sent in Perl
by friedo (Prior) on Jul 19, 2009 at 04:55 UTC
    If you need to capture both STDOUT and STDERR, a more robust option is to use IPC::Open3, which will give you separate filehandles for each.
Re: Checking and verifying a unix command sent in Perl
by Bloodnok (Vicar) on Jul 19, 2009 at 15:23 UTC
    Hmm, is it just me, or does the OP bear the hallmarks of an XY question ??

    A user level that continues to overstate my experience :-))
Re: Checking and verifying a unix command sent in Perl
by leocharre (Priest) on Jul 20, 2009 at 13:18 UTC