|
|
| No such thing as a small change | |
| PerlMonks |
defined ? to be : notby Don Coyote (Monk) |
| on Jun 12, 2012 at 12:40 UTC ( #975767=perlmeditation: print w/ replies, xml ) | Need Help?? |
|
as always a couple of threads make sense when you join them together In chat the subject of length of a defined scalar popped up, and discussions of if else conditional syntax have occured, this article merges parts of those ideas together from the command line, this was being typed
with questions about why zero length scalars remain defined. I quickly suggested that a true conditional be placed as a further argument, to suggest the reason. I went back to this after, and evolved a useful learning trick. Briefly the code suggested in the title is obviously the famous shakespearian quote to be or not to be? Well more rightly it is the answer if Romeo makes the decision, the question itself is an OR conditionial, ok IM getting existential now...
translates to
Best to start with the truth I think. In the original command line the input scalar will always be defined, as it is a scalar passed to the code via the command line. What it will or won't be is true. Lined out for readability.
So lets try this with an undefined scalar. No input from command line and hence no chomp also. We will aslo extended the conditional argument so that the false or 'else' side becomes a further conditional or 'elsif' side. Either way something very interesting happens.
the scalar is undefined with a length of 0. But note it is not 'false'. It is undefined. Which evaluates to false, so it is 'false'. given a string with a length of 0 it will be defined, as a string, of no length and evaluate to false. Given the number 0 it will be defined as a scalar with a length of 1 that evaluates to false. given the string '0', perl internally converts the string to a number and evaluates it as before not being defined, it has no length and evaluates to false. To summarise undefined evaluates to false. '',0, and '0', evaluate to false while being defined, a kind of false positive. And all other defined scalars evaluate to true, a positive positive! So the crux of the play is surely whether the love was ever defined in the first place?! Coyote
Back to
Meditations
|
|
||||||||||||||||||||||||||||||||||||||||||||||