Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

How to get the exit status and the output of the command launched in perl

by sunshine_august (Scribe)
on May 26, 2009 at 08:09 UTC ( #766156=perlquestion: print w/ replies, xml ) Need Help??
sunshine_august has asked for the wisdom of the Perl Monks concerning the following question:

Hi, all monks:

There are two ways to launch system commands in perl, one is by the system operator, and the other is by the backquotes operator. The system operator can only get the exit status of the command, and the backquotes operator can only get the output of the command.

Sometimes I want to get both the exit status and the output of the command, so I can process the output base on the exit status. Usually I do it by this way: first, launch the command by the system operator to get the exit status, then launch the command again by backquotes operator to get the ouput. But this solution is not that good when launching some network commands, because the network status can be changed between the execution of the two commands.

So, my question is: Is there any way I can get both the exit status and the output of the command without launching it twice?

Comment on How to get the exit status and the output of the command launched in perl
Re: How to get the exit status and the output of the command launched in perl
by Anonymous Monk on May 26, 2009 at 08:21 UTC
Re: How to get the exit status and the output of the command launched in perl
by zwon (Monsignor) on May 26, 2009 at 10:13 UTC
    backquotes operator can only get the output of the command
    backticks also can get exit status the same way the system does. You can read it from $?
      yes, but the $? in perl is different from the $? in bash.
      I test the curl command in bash and in perl:
      [larry@august-dev perl]$ curl --max-time 3 http://someweb.org 2>&1 curl: (6) name lookup timed out [larry@august-dev perl]$ echo $? 6
      and if I put it in perl like:
      #!/usr/bin/perl use strict; use warnings; my $result = `curl --max-time 3 http://someweb.org 2>&1`; print "exit status: $?\n"; print "result:\n", $result, "\n";
      then I got:
      [larry@august-dev perl]$ ./foo.pl exit status: 1536 result: curl: (6) name lookup timed out
      How does perl translate the bash $? 6 to its own $? 1536 ?
      Is there a convenient way to translate it back? </code>
        Is there a convenient way to translate it back?

        In short (but not entirely correct), it's 1536 >> 8, i.e. shift right the return value by 8 bits, or divide by 256. For the details, see system.

Re: How to get the exit status and the output of the command launched in perl
by jwkrahn (Monsignor) on May 26, 2009 at 13:14 UTC
    There are two ways to launch system commands in perl, one is by the system operator, and the other is by the backquotes operator.

    And the open operator and the exec operator.   (And do if you consider a Perl program a system command.)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://766156]
Approved by targetsmart
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2014-12-28 06:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (178 votes), past polls