|Welcome to the Monastery|
Re: Can a defined value be of zero length?by kcott (Canon)
|on Apr 20, 2014 at 05:55 UTC||Need Help??|
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.]