kiseok7 has asked for the wisdom of the Perl Monks concerning the following question:
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: what is *undef* ?
by japhy (Canon) on May 25, 2001 at 03:52 UTC | |
Using undef in numeric context (like you did) makes it look like 0; using it as a string makes it look like "". japhy -- Perl and Regex Hacker | [reply] [d/l] |
|
Re: what is *undef* ?
by tachyon (Chancellor) on May 25, 2001 at 06:15 UTC | |
You seem unclear between the difference between defined and false. This code should show you the difference. In perl falseness includes these three miscreants only: 0 "" and undef. All else is true UPDATE: Chipmunk did not like my evals so I have used 5 different perl idioms for if (condition){print this}else{print that}. Choices, choices. TIMTOWTDI TIMTOWODI. To avoid confusion these idioms all do exactly the same thing - test a condition and print someting if it is true and something different if it is not. tachyon
This is about the only printing idiom that does not work! Sadly it is also the most elegant to my eyes.
This only works with a null string and comma after the print as shown above print '', (cond)? foo : bar | [reply] [d/l] [select] |
by Vynce (Friar) on May 25, 2001 at 15:33 UTC | |
or the first uses another set of parens to pass print the right stuff. the second uses perl's mystifying unary plus. unlike some languages, where unary plus does something useless, like take the absolute value of the argument, perl's unary + serves the important function of being a non-parenthesis. this means that things like warn, print, and my_user_func won't be handed the contents of that set of parens as their sole argument list. isn't that something? unary + is nothing. there's nothing like it; all it does is *be* something. .update: mmm, yes, japhy smart. vynce not so smart. obvious answer is, like japhy says, to move the parens to just around defined's argument. but i'm still happy to have gotten to explain the useful use of perl's unary +. . | [reply] [d/l] [select] |
by japhy (Canon) on May 25, 2001 at 15:47 UTC | |
That looks more sensical than some floating + to make things work. japhy -- Perl and Regex Hacker | [reply] [d/l] |
|
Re: what is *undef* ?
by dvergin (Monsignor) on May 25, 2001 at 09:16 UTC | |
It was not $a (equaling zero) which was coerced to equal undef. Just the opposite. It was the undef that, in a numerical context (occasioned by the '==' test), was coerced into being taken as zero. Further evidence of what Perl is doing can be obtained by running with -w in your hash-bang line. Perl will complain: Use of uninitialized value in numeric eq (==) at myprog.pl line 4. and then do its best to render an acceptable result by treating the undef as zero and concluding that the test succeeds. HTH | [reply] [d/l] [select] |
|
Re: what is *undef* ?
by Vynce (Friar) on May 25, 2001 at 15:50 UTC | |
<JOKE>"*undef*" is a more powerful version of the spell "undef"</JOKE> more seriously, undef is a builtin function that removes the definition from a variable, and returns a special value also known, perhaps unfortunately, as undef. as a value, it represents something which hasn't been defined. since things which aren't defined are hard to add or print, perl has to assume something about what to do in those situations. what it does is pretend zero (for addition or other number-based things) or an empty string (for printing or other string contexts). you'll note that adding zero and printing empty strings are pretty safe, sane things to do. admittedly, multiplying by zero can be bad, as can regex-matching empty strings, but hey. you shouldn't use undefined values in those situations anyway. . | [reply] |