Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Obtaining a return code

by Anonymous Monk
on May 17, 2000 at 22:04 UTC ( [id://12195]=perlquestion: print w/replies, xml ) Need Help??

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

I am running Perl 5 in DOS, is there a way of getting the O/S return code once an action has been initiated?

Replies are listed 'Best First'.
Re: Obtaining a return code
by infoninja (Friar) on May 18, 2000 at 20:34 UTC
    At least on a Unix box, system() will return the exit status of the system call:
    $x = system("echo y"); $x = $x/255; #set $x to actual exit value $y = system("blahblah"); $y = $y/255; #set $y to actual exit value
    results in $x set to 0 and $y set to 256 (as long as there's no executable called "blahblah" in $PATH).
Re: Obtaining a return code
by chromatic (Archbishop) on May 21, 2000 at 08:11 UTC
    The special variable $? holds the status of the backtick or system operation. If you're really curious, you can do: my $sig_number = ($? & 255); to see which signal killed the process, if any. It's not all that useful, though, for system(), as you can get the return value anyway (as turnstep points out below).

    Which return code are you expecting?

      But $? is the same thing as just reading the return value yourself, right? At any rate, $? on my Win32 system doesn't show anything except "0", no matter what is in the system call:
      $rv = system("uname"); print "Returned a $rv and \$? is $?\n"; $rv = system("ver"); print "Returned a $rv and \$? is $?\n";

      Unix produces:

      SunOS
      Returned a 0 and $? is 0
      Returned a 65280 and $? is 65280
      

      Windoze produces:

      Bad command or file name
      Returned a 0 and $? is 0
      
      Windows 98 [Version 4.10.1998]
      
      Returned a 0 and $? is 0
      
Re: Obtaining a return code
by bdimych (Monk) on Dec 10, 2007 at 15:25 UTC
    From perldoc -f system:
    You can check all the failure possibilities by inspecting $? like this:
    
       if ($? == -1) {
           print "failed to execute: $!\n";
       }
       elsif ($? & 127) {
           printf "child died with signal %d, %s coredump\n",
               ($? & 127),  ($? & 128) ? 'with' : 'without';
       }
       else {
           printf "child exited with value %d\n", $? >> 8;
       }
    
    

    See also: $?

    and

    POSIX system

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://12195]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-03-29 01:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found