in reply to why can't compare a variable with undef directly?
Most of the time, the way perl works will just make your work easier, and Do What You Mean. For exemple, in C, if you have an int with value 42, and the string "42", you have to convert either one of them to the other format. The == operator (and all numeric operators) makes it simpler by implicitly converting both sides to numbers, this way you don't have to care if it was user input, extracted from a string, the output of an operation on other numbers or another source, you can just do 42 > "40" and perl will understand what you mean and compare 42 and 40.
Perl is good at guessing most of the time, but it's not psychic, so when you try undef == 0, it will use the implicit meaning of undef ("nothing"), and compare "nothing" to 0, and tell you that's the same thing. That's why you have to write explicitly that you want to know wether a variable is defined or not.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: why can't compare a variable with undef directly?
by RonW (Parson) on Dec 10, 2014 at 21:44 UTC |