Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Boolean (was Boolian) operators defy my understanding...

by philcrow (Priest)
on Jun 20, 2007 at 14:38 UTC ( [id://622261]=note: print w/replies, xml ) Need Help??


in reply to Boolean (was Boolian) operators defy my understanding...

system is odd. It returns the exit status. By tradition, status zero means success (which happens to be false), while failures are reported by a number that you can look up to see what went wrong (these are non-zero and therefore true).

Sometimes it helps to name the return value:

my $failure = system...; if ( $failure ) { ... }

Phil

The Gantry Web Framework Book is now available.

Replies are listed 'Best First'.
Re^2: Boolean (was Boolian) operators defy my understanding...
by cgmd (Beadle) on Jun 21, 2007 at 00:10 UTC
    This now seems to be making sense to me, so please correct me if I'm wrong:

    A successful system call yields the bash exit code of "0", and the Perl interpretation of this "0" (as being a failure), permits execution of the "other" component of the || comparison operator.

    On the other hand, an unsuccessful system call produces an exit code other than "0", and is erroneously interpreted by Perl as "true". This accounts for Perl's action of allowing an unsuccessful system call to stand in as true for the first component of the "&&" operator.

    Please excuse me if this comes across as a lame discussion, but this issue has been difficult for me to understand.

    Thanks!

      Yes, you've got it. That's why I said system is odd. Most Perl functions return something that works in a boolean test (open ... or die), but not system. It uses the Unix shell behavior (all shells do this not just bash). So you have to reverse the sense of the test at very least. Really, you should know what the failure codes are and handle them as cases, as shown in another response.

      Phil

      The Gantry Web Framework Book is now available.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-03-28 13:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found