Posted by Malcolm Heath to the GeekTalk MailList (original thread) here's some stuff on how to do bitwise comparisons with Perl Error Codes:

Just in case anyone else was confused by this "bit math" return status stuff, Dave and I sat down and looked it up, and here's a brief tutorial on inspecting $? using bitwise operators:

First off, the operators:

a >> b shift a to the right by b bits.

Therefore

32 >> 4 = 2

32 is 00100000 in binary, and so shifting it right 4 bits gives you 00000010, or 2.

a & b bitwise AND a and b.

The way an AND works is described below:

a AND b  =  result 
0     0     0
0     1     0
1     0     0
1     1     1

So, if a is 32 and b is 160, a AND b is 32.

a = 32 = 00100000 
b = 160= 10100000
         --------
         00100000

In Dave's example, if the value of $? is 256, we get the following values:

$exit_value = 256 >> 8, or 1 
$signal_number = 256 & 127, or 0
$dumped_core = 256 & 128, or 0

You can see the first 1025 possible combinations by doing something like this:

# perl -e 'for($foo=0;$foo<=1024;$foo++){ printf("%d: %d\t%d\t%d\n", $foo, $foo >> 8, $foo & 127, $foo & 128);}' 

365, for example, would tell you that I returned 1, dumping core on a signal 11.


CategoryProgramming

PerlErrorCodes (last edited 2003-11-23 08:59:55 by AdamShand)