$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
my $exval = $? >> 8;
print "printing exit value: $exval \n";
if ( $exval == 0 ) {
            print "false cat command did not happen\n";
    {else}
            print "$exval is: $exval \n";
     }
}


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



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




[EMAIL PROTECTED] wrote:
> It is difficult for me to bottom post as I am using Lnotes.  Sorry.
>

See previous rants about this.....

> ok so yes In general,  I thought:
>
> SHELL 0= true
> Perl 0 = false
>
> SHELL 1 = false
> Perl 1 = true
>
> what do you mean  "non zero is false in this context."  b/c in general I
> thought as above.  Yes it is opposite in Perl as above.
>

But that is not what your test said. What you listed above is correct,
but in your code you had:

if ($extval == 0) {
   print "False...."
}

Which is opposite from above.

> Finally, when do I use my $exval = $? >> 8 as opposed to my $exval = $?
>>
> 127.  And what is the diff?
>

I am not sure when this changed, but the docs have become IMHO more
difficult to read. perldoc -f system describes how to extract the data
from $?.  Essentially $? encodes using bit switches three values, the
exit value of the called program, the signal number that killed it, and
whether it dumped core. In older Perl's system's documentation listed
that as,

                    $exit_value  = $? >> 8;
                    $signal_num  = $? & 127;
                    $dumped_core = $? & 128;

Why this was switched to use the more difficult to read new format
wrapped up in an if/elsif/else (with a ternary printf!!) I have no idea.
Just *adding* the example would have been a much better idea to me.

So in your case to get the 'exit value' you use the >> 8 shifter and
then check for a non-zero value. zero is success, non-zero is failure
(in most applications!!! this is only convention!). People often check
$? against non-zero/zero because if the program dumped core or exited
from a signal the value would non-zero indicating failure. However, it
is possible (I suppose) for a program to exit cleanly with an exit value
of 0, from either a signal and/or while dumping core, which is why it is
"better" to check the exit value. It is also possible for a program to
exit successfully with a non-zero value, but it better be documented and
I wouldn't suggest doing this (as it breaks convention).




-- 
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