understood!

thanks,





                                                                           
             "Wiggins                                                      
             d'Anconia"                                                    
             <[EMAIL PROTECTED]                                          To 
             .org>                     [EMAIL PROTECTED]              
                                                                        cc 
             02/28/2005 01:05          Perl Beginners <beginners@perl.org> 
             PM                                                    Subject 
                                       Re: return code                     
                                                                           
                                                                           
                                                                           
                                                                           
                                                                           
                                                                           




[EMAIL PROTECTED] wrote:
> $exit_value  = $? >> 8
> $signal_num  = $? & 127;
> $dumped_core = $? & 128;
>
> ok this helps, thanks!  But, in my small block of code I am using $? >> 8
> as so
>
> system ("cat /tmp/used");                 #  /tmp/used does not exist so
> this should return 0 for false in Perl and 1 false in Shell

Nope, because $? is just a way to access what the shell returned. So $?
is in the shell context, not Perl context. So C<system> returns true
because 'cat' does run (aka the shell didn't fail to execute the program
-- permission error, command not found, etc.).  But it is 'cat' that
exited (the key here is the difference between failing to run (perl
context) and running, but exiting with a non-zero value (shell context)).

> my $exval = $? >> 8;

So the above is the *exit value* of the shelled command, cat.

> print "printing exit value: $exval \n";
> if ( $exval == 0 ) {
>             print "false cat command did not happen\n";

This is incorrect, the 'cat' command *did* happen, it failed, but it did
execute!!

>     {else}
>             print "$exval is: $exval \n";
>      }
> }
>

my $return = system('cat /tmp/used') or die "'cat' didn't run at all!";
my $exval = $? >> 8;

if ($exval) {
    warn "'cat' ran but exited unsuccessfully: $exval";
}
else {
    warn "'cat' succeeded: $exval";
}

Of course 'cat' is a poor example, because you would have to capture the
output, and to do that you need to use backticks rather than 'system'.
(Not to mention it would be silly to shell out to cat since Perl can
just open the file.)

>
> AND it is returning 2???  WHY?
> The output is below:
>
> linect is not less than 2: 3
> cat: cannot open /tmp/used
> printing exit value: 2
> true exit value is: 2
>

'2' is cat's way of saying I couldn't find a file to read so I am
bailing. It is program specific (but will often use the system
conventions as a guide).

[snip]

For those reading along *THIS* is why it is *always* better to handle
something in Perl directly than to shell out to the system.  If you want
to read a file use 'open', if you want to list files in a directory use
'opendir' (or a glob), etc.  The code will almost always be longer to
shell out, assuming you do it correctly!

http://danconia.org



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to