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

Hi!

How I can get system-function executed program return value if it is negative? Is it possible at all? I noticed that only values 0..255 are possible to get, negative was always 255. So it looks like system-function handles values only as unsigned int. What is other simple way as system-function to execute external program and get correct return value (-n..+n)? I use script on W2k cmdprompt. Thanks.

  • Comment on How to get system -function negative return value?

Replies are listed 'Best First'.
Re: How to get system -function negative return value?
by Abigail-II (Bishop) on May 27, 2004 at 09:14 UTC
    System function return 8 bit values. Depending whether you treat the values as signed or unsigned, they are in the range -128 .. 127, or 0 .. 255. When doing system from Perl, the return value of the called program is the second least significant byte of the return value of system (and also available in $?). Since only the two least significant bytes are used (the least significant byte contains information about signals and core dumps), shifting 8 bits will give you the return value - unsigned.

    To get the signed value, do something like:

    my $ret_val = unpack c => pack C => ($? >> 8);

    Abigail

Re: How to get system -function negative return value?
by Happy-the-monk (Canon) on May 27, 2004 at 08:18 UTC

    Maybe this helps:

    The return value is the exit status of the program as returned by the [id://perlfunc:wait|wait()] call. To get the actual exit value divide by 256. See also exec.

    See perldoc -f system

    See also Perl 5.8.4 perldoc system where the new page says: "To get the actual exit value shift right by eight (see below)."

      Thanks, but I exactly ment already divided value :-(

      Code:

      my $return_value = system("test.exe something");
      returns 0-65280, negative value is always 65280

      and when divide it

      $return_value = $return_value/256;
      it returns naturally 0-255

      Maybe I should use some other function. Any suggestions?

        As I said in another post:
        $ perl -wle 'print unpack c => pack C => 65280 >> 8' -1

        Abigail

Re: How to get system -function negative return value?
by dfaure (Chaplain) on May 27, 2004 at 09:17 UTC

    Using these two test progs:

    process.pl

    #!perl use strict; use warnings; my $exitCode = shift; print "will exit with $exitCode\n"; exit($exitCode);

    shell.pl

    #!perl use strict; use warnings; sub run_proc { my $code = shift; print "now running with $code\n"; my $status = system("process.pl $code"); my $exit_value = $status >> 8; my $signal_num = $status & 127; my $dumped_core = $status & 128; print <<"END_OF_STATUS"; exit_value: $exit_value signal_num: $signal_num dumped_core: $dumped_core END_OF_STATUS } run_proc(123); run_proc(-1); run_proc(255); run_proc(-5); run_proc(251);

    I obtain these results with ActivePerl 5.8.3.809 on Windows 2000 (binary release):

    now running with 123 will exit with 123 exit_value: 123 signal_num: 0 dumped_core: 0 now running with -1 will exit with -1 Can't spawn "cmd.exe": No such file or directory at c:\Exit value\shel +l.pl line 10. exit_value: 255 signal_num: 0 dumped_core: 0 now running with 255 will exit with 255 exit_value: 255 signal_num: 0 dumped_core: 0 now running with -5 will exit with -5 exit_value: 251 signal_num: 0 dumped_core: 0 now running with 251 will exit with 251 exit_value: 251 signal_num: 0 dumped_core: 0

    The '-1' return code seems to have a special meaning. Sorry, I couldn't see why looking at perl's source code... You should give your perl -V results.

    Anyway, I could get negative return code...

      Doesn't help.. :(

      I tested and noticed that when process is perl-module, it works like that way. But when process is c-module exe, negative return value is always same. Also This must work with return values between about -99999 to 999999. With perl module range was 0 to 255, both positive and negative values, so never know which original value is. Maybe I have to try solve this with win32::process..