Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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.


Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by responing to this node).


In reply to Re: variable set to 0 ? 0 : 1 by theorbtwo
in thread variable set to 0 ? 0 : 1 by c

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-18 17:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found