Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Understanding $?

by rjt (Deacon)
on Mar 07, 2013 at 01:24 UTC ( #1022129=note: print w/ replies, xml ) Need Help??


in reply to Understanding $?

For a more thorough treatment of what $? will contain, have a look at POSIX's macros for WIFEXITED and WEXITSTATUS. The canonical (but admittedly not always followed!) way of checking an exit status is something like:

my $output = `echo hello`; # Output contains "hello" if (WIFEXITED($?)) { print("Command exited with status " . WEXITSTATUS($?)); } else { print("Command exited abnormally"); # If desired, use the other macros to figure out which # signal killed the process. }

Obviously in the above contrived example with echo, it's highly unlikely to exit abnormally, but with more complex commands, it can be very helpful to know what killed the sub-process, and what the exit status was.

Of course, the $output variable will contain the actual standard output (STDOUT) of the command itself, if there was any. If you need to capture STDERR in the same variable as well, you can add 2>&1 to the end of your command. If you need to capture STDOUT and STDERR separately, you would need to use pipes (a la IPC::Open3 rather than the simple backticks (qx) operator, or redirect the streams to File::Temp files and read them in after the command finishes.


Comment on Re: Understanding $?
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (8)
As of 2014-10-01 14:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (27 votes), past polls