in reply to variable set to 0 ? 0 : 1
This statement will return 0 if the $status variable is zero, or 1 if it is not. I'm not sure if you don't understand the precidence issues here, or if you don't understand what the ?: operator is. If it's the former, B::Deparse is often good for answering these sorts of questions; the correct precidence is return((($status == 0) ? 0 : 1));. I show how to derive this in Re: Ternary Operator: Condition-Negation.
If it's the later, I suggest reading about the Conditional Operator in perlop. In short, the conditional operator (also called the ternary operator, since it's the only three-valued operator in common (programming) use) is a strange form of if, written as conditional ? if-true : if-false. If conditional is true, the value of the operator is if-true, otherwise the value is if-false.
In the spirit of TIMTOWTID, these stanzas are all (nearly) equivlent:
return $status == 0 ? 0 : 1; return !!$status; # Checks if $status is true or not, not if it is e +xactly zero; "" or undef are false, but not == 0. if ($status == 0) { return 0; } else { return 1; }
Probably, in the code $status ends up being a count, but the implementor only wanted to gaurntee that if $status is nonzero, a true value is returned, so they could change the function to do less work in the future.
Update: fixed typo. Thanks, blyman.
|
|---|