G'day j0se,

You're quite correct in noticing that there's something not-quite-right about that code; however, I believe you're asking the wrong questions.

My understanding of the intent of that code is to keep processing user input while the user enters patterns. When the user enters no pattern, i.e. just hits the Enter key (or equivalent), then processing should stop.

Take a look at each of these:

So, if the user just hits the Enter key, $pattern is assigned "\n" and, after the chomp, it will have the defined, FALSE value of "". The defined $pattern is redundant because it will always be TRUE. Instead of asking:

"why would I need to check for the length of the string? Is it not enough to check for the "definedness"?"

A better question might be the reverse of that:

"Why would I check for definedness? Is it not enough to check for length?"

And, in this case, the answer would be: "Yes, checking for length is sufficient."

It's often useful to start a series of chained boolean tests with defined $whatever and as it both short-circuits any further tests (if FALSE) and avoids (potentially copious) "uninitialized value" warnings. In this particular instance, defined is always TRUE and, therefore, never short-circuits; and length accepts undef as an argument without issuing a warning.

Using length in a test is often used to differentiate the FALSE value "0" (which is valid in some context) from the FALSE value "" (which is invalid in the same context).

[Completely off-topic: I noticed in your home node that you're keeping a history of your Monk levels. You have "unknown" against "Initiate"; you start at that level when you first register so you can fill that in with 2011-04-10. See Voting/Experience System.]

-- Ken


In reply to Re: Can a defined value be of zero length? by kcott
in thread Can a defined value be of zero length? by reisinge

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!
  • 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, details, 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, summary, 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.