Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting information from $?

Tags:

perl

Can you please provide good explanation about the following perl code snippet. I got some idea from google but still lots of basic confusion is there. great help if you can provide small notes on it

$exit_value  = $? >> 8;
$signal_num  = $? & 127;
$dumped_core = $? & 128;
like image 994
Rajesh Kumar Avatar asked Mar 01 '12 08:03

Rajesh Kumar


1 Answers

Quoting The Doc:

$?

The status returned by the last pipe close, backtick (`` ) command, successful call to wait() or waitpid(), or from the system() operator. This is just the 16-bit status word returned by the traditional Unix wait() system call (or else is made up to look like it). Thus, the exit value of the subprocess is really ($?>> 8 ), and $? & 127 gives which signal, if any, the process died from, and $? & 128 reports whether there was a core dump.

>> 8 gives us the higher byte of a 16-bit word.

& 127 is essentially the same as & 0b01111111, giving out the lower 7-bit part of that word.

& 128 is the same as & 0b10000000, which is basically checking for the 8th bit of the result.

like image 151
raina77ow Avatar answered Sep 30 '22 04:09

raina77ow