Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re: Can a defined value be of zero length?

by kcott (Chancellor)
on Apr 20, 2014 at 05:55 UTC ( #1082932=note: print w/replies, xml ) Need Help??

in reply to Can a defined value be of zero length?

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

Replies are listed 'Best First'.
Re^2: Can a defined value be of zero length?
by reisinge (Friar) on Apr 21, 2014 at 10:28 UTC
      "Have you considered ikegami's answer ..."

      The length v5.10.1 documentation doesn't specifically mention undef but the length v5.12.0 documentation does. (I can't find any mention of this change in any of the deltas for those versions.)

      I'm not in a position to test that (I have several versions availble but 5.12.3 is the earliest); however, assuming that's correct, using defined before length would probably be a good idea if using one of those earlier versions.

      -- Ken

        I can't find any mention of this change in any of the deltas
        Check the ninth bullet under Other potentially incompatible changes
        لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1082932]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2017-10-19 17:06 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (255 votes). Check out past polls.