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?

-1 is not an 'exit status' of a system() call. -1 means system() was unable to execute the specified command. Because the command never executed, it did not generate an exit status.

EDIT: Apparently the -1 return value has some twists to it. The perl system() docs say that system() returns the return value of wait(). The perl wait() docs say:

Behaves like the wait(2) system call on your system: it waits for a child process to terminate and returns the pid of the deceased process, or "−1" if there are no child processes. The status is returned in $? and "${^CHILD_ERROR_NATIVE}". Note that a return value of "−1" could mean that child processes are being automatically reaped, as described in perlipc.

The perlipc docs say:

On most Unix platforms, the CHLD (sometimes also known as CLD ) signal has special behavior with respect to a value of 'IGNORE' .... Calling wait() with $SIG{CHLD} set to 'IGNORE' usually returns -1 on such platforms.

As far as I can tell, if you set $SIG{CHLD} = 'IGNORE', then you have told perl you don't want to wait for any children to finish executing. In that case, system() executes the command, but system() immediately returns -1 because your program is too impatient to wait and get the exit status from the child.

Therefore, system() can return -1 when system() successfully started the command.

For the elsif expression, why are they bitwise anding it with 127? 127 in binary is 0b0111_1111

That checks whether any of the first 7 bits is 'on'.

So, that's only giving me the bottom 7 bits of $?, not 8. Why throw away that top bit in the bottom byte?
Presumably, the agreed upon protocol is to use only the first 7 bits to indicate which signal terminated the program. In the old ascii system, the maximum number of characters that could be represented by one byte was 128 (0-127) because the 8th bit was used for other purposes. Maybe that is why the protocol uses only the first 7 bits to indicate which signal terminated the process.

In addition, I can imagine a system where there were only 2 possible signals. With that protocol, you would only need to write ($? & 3) to determine if a signal terminated the process. Maybe there were only 7 signals when the protocol was decided upon?


In reply to Re: return value from system call, exit status, shift right 8, bitwise and, $? by 7stud
in thread return value from system call, exit status, shift right 8, bitwise and, $? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.