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

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

by zwon (Abbot)
on May 26, 2009 at 10:13 UTC ( #766182=note: print w/replies, xml ) Need Help??


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

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 $?
  • Comment on Re: How to get the exit status and the output of the command launched in perl
  • Download Code

Replies are listed 'Best First'.
Re^2: How to get the exit status and the output of the command launched in perl
by sunshine_august (Scribe) on May 26, 2009 at 10:31 UTC
    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://766182]
help
Chatterbox?
[Corion]: Once upon a time I had automatic tests for checking the synopsis, but I stopped doing that because the setup was too fragile on CPAN testers for extracting code from the SYNOPSIS.
[Corion]: Maybe I should move the extraction of the code from the SYNOPSIS section into the author tests, or something like that...
[choroba]: Corion Sounds reasonable
[Corion]: choroba: Yeah - I basically have the same for regenerating README and README.mkdown already, except that I do that in Makefile.PL, but I guess one or the other thing should somehow work ;)
[ambrus]: was this bug: https://rt.cpan. org/Public/Bug/ Display.html?id= 59814
[Corion]: ambrus: Oh - that one would be much harder to automate... The SYNOPSIS section should mostly be a runnable program IMO, but I write only small snippets in my documentation for single functions/methods, and creating the appropriate environment for ...
[Corion]: ... those in an automated fashion seems somewhat hard to me. Although it should do wonders for the test coverage ;)

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (10)
As of 2017-02-27 12:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?






    Results (383 votes). Check out past polls.