http://perldoc.perl.org/perlvar.html $? - 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 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. (Mnemonic: similar to sh and ksh.).
wait() man page: http://bama.ua.edu/cgi-bin/man-cgi?wait+2 o If the child process stopped, the high order 8 bits of status will contain the number of the signal that caused the process to stop and the low order 8 bits will be set equal to WSTOPFLG. o If the child process terminated due to an _exit() call, the low order 8 bits of status will be 0 and the high order 8 bits will contain the low order 8 bits of the argument that the child process passed to _exit(); see exit(2). o If the child process terminated due to a signal, the high order 8 bits of status will be 0 and the low order 8 bits will contain the number of the signal that caused the termination. In addition, if WCOREFLG is set, a "core image" will have been produced; see signal(3HEAD) and wstat(3XFN). > 1. What does ($? & 127) mean? 127 in binary is 0111 1111. It is used as a mask. ($? & 127) returns a number between 0 and 127, as any higher bits get zeroed out. If the child terminated due to a signal, ($? & 127) will give you the signal code that stopped it, because the signal code is stored in the lower 8 (er, 7?) bits of the status as per the wait man page. 128 in binary is 1000 0000. ($? & 128) tells you if the 8th bit is 1 or 0, indicating coredump or not. > 2. What does $? >> 8 mean? >> is a binary shift. >> 8 shifts the status 8 bits right, tossing out the lower 8 bits and giving you the upper 8 bits. If you do (ABCD EFGH IJKL MNOP >> 8) (where each letter represents a single 0/1 bit) you get ABCD EFGH. As explained in the wait man page, if the child terminated due to an _exit() call, the high order 8 bits of the status will contain the low order 8 bits of the argument that the child process passed to _exit(). So we shift the status 8 bits right to get the value the child passed to _exit(); the exit code. I'm sorry if this confuses you and would be glad to explain any part you didn't understand. This is sort of the type of stuff more commonly found in C. On 9/4/07, Dan Sopher <[EMAIL PROTECTED]> wrote: > Regarding the following document: > http://perldoc.perl.org/functions/system.html > > 1. What does ($? & 127) mean? > 2. What does $? >> 8 mean? > > if ($? == -1) { > print <http://perldoc.perl.org/functions/print.html> "failed to > execute: $!\n"; > } > elsif ($? & 127) { > printf <http://perldoc.perl.org/functions/printf.html> "child died > with signal %d, %s coredump\n", > ($? & 127), ($? & 128) ? 'with' : 'without'; > } > else { > printf <http://perldoc.perl.org/functions/printf.html> "child > exited with value %d\n", $? >> 8; > > } > > Thanks in advance, > > Dan -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
