Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Comment on

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

I subscribe to this opinion as well.

To me, undef could mean a couple of things, depending on context:

  • if a function returns it, the function means to tell me
    • there's nothing more to tell you (e.g. when <$filehandle> has read the last line of the file, or when each %hash has returned the last key/value pair of the hash)
    • something went wrong (e.g. "you asked me to list your appointments on Feb 30th, but I couldn't find that date in my calendar?")
    • it wants to return a false value, where 0 or "" don't cut it (e.g. if you use some module to log in to your Amazon account and get the number of items on your wish list, it would be bogus for $amazon->items_on_wish_list to return 0 when $amazon->log_in(user => "muba", password => "soopr sekret!111") failed. 0 here would mean that my wish list is empty, whereas undef would mean "failed to fetch wish list")
    • it pulled its return value from some external source (a JSON object, a database record, whatever) and needs a way to represent the external source's notion of null or whatayamaycallit)
  • if I set a value to undef manually, I could do that because
    • I haven't come around to set it to something more definite yet (e.g. in writing code that reads one line from a file at a time, but has to remember the previous line it read, I'd write
      my $previous = undef; while (my $line = <$filehandle>) { chomp $line; ...; # process $line $previous = $line; }
      because initially, there is no previous line yet, and setting $previous to "" initially would make it look like the previous line was just an empty line. There is a difference between "no previous line" and "previous line was empty")
    • I need it to exist, but I really don't care about the value right now (e.g. I'm preparing the data for a to-be-created user account, which will have a birthday column, but I really don't need to set a definite value for that column while creating the account. The user can set that whenever she fees like. So I'll leave it undef for now)
    • I'm writing a function and I want to return undef for any or more of the reasons I mentioned above

What all of these reasons have in common, is that they essentially mean to represent mu.

If asked the question, "did you stop beating your wife?" then the answer "no" would imply that you still beat your wife. "Yes" doesn't cut it either, because it directly means that you used to beat her. But "mu" or "undef" would mean, "the question does not apply, I have never beaten her."

Therefore, I think that if you're about to do something with a variable (print it, interpolate it, do arithmetic operations with it, throw it against a wall, feed it to your dog), you should either make sure it isn't undef, or explicitly check for its definedness.

In reply to Re^3: How come undef eq '' ?? by muba
in thread How come undef eq '' ?? by tmharish

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others wandering the Monastery: (11)
    As of 2018-06-25 10:32 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (126 votes). Check out past polls.