in reply to check undef
When comparing a value against a constant, always write the constant to the left side like this:
if (undef = $WCkey) { ...
If you make the error you made in your original post, Perl will immediately tell you.
Also, the function for checking whether a value is undef or not is the defined function.
Update: The if (undef = $WCkey) { ... is not a typo but intended for demonstration. Perl raises a runtime error when you use
if (undef = $WCkey) { ...
instead of
if (undef == $WCkey) { ...
while it doesn't even raise a warning if you write the "comparison" the other way around:
>perl -we "if (undef = $foo) { print 'bar' }" Name "main::foo" used only once: possible typo at -e line 1. Modification of a read-only value attempted at -e line 1. >perl -we "if ($foo = undef) { print 'bar' }" Name "main::foo" used only once: possible typo at -e line 1.
So, in the spirit of building a defensive habit, write the constant to the left side of comparisons for equality.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: check undef
by Sun751 (Beadle) on Jun 23, 2009 at 07:32 UTC | |
by Corion (Patriarch) on Jun 23, 2009 at 08:34 UTC |