Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

comment on

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

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:

  • perlsyn: Truth and Falsehood: note all the values which are considered to be FALSE.
  • defined: note that this returns a boolean value (which will always be TRUE unless its argument evaluates to undef).
  • length: note that this accepts undef as an argument.

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":



  • 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 imbibing at the Monastery: (4)
As of 2024-04-19 05:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found