|Just another Perl shrine|
Re: How come undef eq '' ??by muba (Priest)
|on Jan 30, 2013 at 15:52 UTC||Need Help??|
Because perl does a lot of automatic data type coercion for you if it's needed.
If undef didn't stringify to "", then what output should this produce?
In your last code snipped,
what output did you expect? Why?
The reason you're getting "Out" here is not because of how undef stringifies, it's because of undef being considered false. So in the expression $tmp and ( $tmp eq "" ), the and operator looks at its left-hand side, concludes that it's false, and so returns false, causing your if-else to jump to the else part right away. Whether or not ($tmp eq "") is never evaluated in that snippet.
Addendum: the raison d'être of undef is that it can convey the meaning of "there is no valid value for what you asked for," for example if you have a function that returns the next paragraph from some document, then "" would mean that the next paragraph is empty, and undef would mean that there is no next paragraph.