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

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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1022129]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (4)
As of 2017-11-20 02:22 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (283 votes). Check out past polls.