Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Understanding $?

by rjt (Curate)
on Mar 07, 2013 at 01:24 UTC ( [id://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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-18 23:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found