|Perl Monk, Perl Meditation|
return value from system call, exit status, shift right 8, bitwise and, $?by Anonymous Monk
|on Feb 11, 2010 at 03:30 UTC||Need Help??|
Anonymous Monk has asked for the
wisdom of the Perl Monks concerning the following question:
I tend to use the system() function pretty often, and I want to make sure I detect any problems. So, I think this means I want to make sure that programs I run always have an exit status of 0.
My understanding of the system function is that it returns a 16-bit value, where the high 8 bits are the exit status, and the low 8 are the signal id. Is that correct?
It also seems that I don't actually have to save the exit status after every use of system(), since the value also gets stored in $?.
I don't expect the programs I'm running to be killed by any signals (unless I'm the one doing the sending) ... So, I think this means I'm only concerned that the high 8 bits are all off. Therefore, does this mean I should be carefully making my system calls like this:
or would the following suffice? :
Also, I notice in the docs for system(), there's this:
For the if expression, if the exit status is 16 bits, then the value goes from 0 to 655535. What does it mean to check if it's -1?
For the elsif expression, why are they bitwise anding it with 127? 127 in binary is 0b0111_1111. So, that's only giving me the bottom 7 bits of $?, not 8. Why throw away that top bit in the bottom byte?