Pathologically Eclectic Rubbish Lister | |
PerlMonks |
Re: Re: Re: Re: Re: Simplifying code (Not obfuscation)by Molt (Chaplain) |
on Mar 04, 2003 at 13:56 UTC ( [id://240342]=note: print w/replies, xml ) | Need Help?? |
No, you're quite right. I was being stupid and messed up, will correct that in a second so as not to confuse others. The precedence issue is moderately easy to explain in this context. There's a standard Perl idiom open FILE, "file.txt" or die "Error: $!\n".This is the equivilent to open (FILE, "file.txt) or die "Error: $!\n"as the 'or' is evaluated after the open command. If you were to try open FILE, "file.txt" || die "Error: $!\n"you may be surprised to know you'll never get the error message, this is because the || operator is evaluated before the open and so this is actually equivilent to open FILE, ("file.txt" || die "Error: $!\n")and so would only die if the string literal "file.txt" was blank. Not good. You get a similar thing with thing happening quite often. Moral of this story: Generally use || for defaulting values ($a = $b || $c), but use 'or' for logic control similar to the above. Oh yeah, and make sure your code dies when it gets an error, not when it doesn't.. I seem to have failed on this one
In Section
Seekers of Perl Wisdom
|
|