f50bodykit has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I've figured out what most of this does. I'm wondering what the last line is doing and where I can read more about it. Its a snippit from a perl script used to run a command in windows.
my $result = qx($CommandGoesHere); $rc = $?; $rc = $rc >> 8 unless ($rc == -1);
in particular the $rc >> 8 And why it is the number 8.

I tried searching here and google but havent had much success due to it being code. Any links to where I can do some further reading would be greatly appreciated.

Thank you,

Jason

Replies are listed 'Best First'.
Re: Return code from calling qx
by ikegami (Patriarch) on Oct 27, 2009 at 19:19 UTC

    The docs clearly answers your question. $? is

    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. (Mnemonic: similar to sh and ksh.)

      The docs may explain it, but trying Googling for qq{perl "$? >> 8"} sometime. Google doesn't even honor the phrase so the results are not that impressive. On Google it's hard if not impossible to search for "$? >> 8? -- try it without "perl" and you will get links to the number 8 because Google is not searching on the phrase even though it is in quotes (probably too many special characters). One more reason not to like the five bazillion special two character variables Perl has (of course, when typing them in, most of us really like them! :-)

      Elda Taluta; Sarks Sark; Ark Arks

        I fail to see your point, especially since "special perl variables" turns up the very text I quoted in the first result.
Re: Return code from calling qx
by Corion (Patriarch) on Oct 27, 2009 at 19:06 UTC
Re: Return code from calling qx
by jettero (Monsignor) on Oct 27, 2009 at 19:28 UTC
    Corion and ikegami are correct. But I can never remember all the weird little things with qx and system (et al); so I just use IPC::System::Simple and all my problems are solved for me.

    -Paul

Re: Return code from calling qx
by gmargo (Hermit) on Oct 27, 2009 at 19:34 UTC

    See also the 'system' function details in perlport. There they discuss why you should not use the hard coded constants, but instead use the WIFEXITED and WEXITVALUE macros provided by POSIX.

    This was discussed in a recent thread 801643 regarding interpreting the results of waitpid.

Re: Return code from calling qx
by f50bodykit (Initiate) on Oct 27, 2009 at 19:54 UTC
    Thanks everyone I found all of your refrences helpful. I'm having trouble understanding shift operators. When I run
    qx(dir thisdoesntexist); print $?;
    I get
    File Not Found 256
    Running
    qx(dir thisdoesntexist); print $?>>8;
    returns
    File Not Found 1
    What does it mean to be shifting bits by 8 and how did that change the return code to 1?

    Thank you,

    Jason
      Think in terms of binary:
      100000000 (binary) = 256 (decimal)
      If you shift it 8 bits to the right (>> 8) you get:
      1 (binary) = 1 (decimal)
      Hope this helps a little.

      -John
        Yes that helps. Thanks John.