Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

Those are both great examples and show why unkown is better than undef. First, keep in mind that undef behavior is rather ad-hoc and poorly specified and the warnings reveal that it's not designed for comparison. However, unknown is specifically designed for comparison and nothing else and it's behavior is well documented in my module.

In this case, either of your examples would be a bug if you're dealing with either undef or unknown values. However, unknown values offers safety. Let's look at your first example:

if ( $salary < $threshold ) { increase_salary( $employee, 3_000); } else { decrease_salary( $employee, 3_000); }

What might reasonably happen if we have an undef value? The salary is coerced to zero and that's probably less than the threshold, thus causing increase_salary() to be called. What happens in there? Presumably something like this:

    $employee->salary( $employee->salary + $increase );

And an employee who's salary was previously unknown now has a salary of $3,000 and with undef values, you've probably corrupted your data.

What happens if you use unknown? Well, salary does not evaluate as less than threshold, so we call decrease_salary() and probably still have a bug, right? In that function, we probably hit code like this:

    $employee->salary( $employee->salary - $decrease );

So did we corrupt our data? Nope. Remember, unknown values are designed to provide semantically correct comparisons and nothing else. What happens if you try to do something else? For the example above, you see Math cannot be performed on unknown values followed by a stack trace.

In other words, unknown values will throw an exception rather than allow your data to be corrupted.

So should you test for unknowns? Sure. The module exports an is_unknown predicate (which defaults to $_). Using that liberally will help make your code more robust. However, if you forget (and which programmer doesn't forget from time to time?), undef can corrupt your data while unknown will die rather than allowing it to be corrupted. That's a deliberate design goal.

The only case where I've violated this rule is stringification: it prints [unknown] for unknown values. However, this may have been a mistake (imagine printing this in JSON, for example) and I may revert that behavior in another release.


In reply to Re^2: "undef" is not NULL and what to do about it by Ovid
in thread "undef" is not NULL and what to do about it by Ovid

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others imbibing at the Monastery: (4)
    As of 2014-07-26 14:09 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (177 votes), past polls