"be consistent" | |
PerlMonks |
The Swiss Army Chainsaw... Watch out for your legs there, buddy!by crashtest (Curate) |
on Jul 25, 2006 at 05:21 UTC ( [id://563426]=perlmeditation: print w/replies, xml ) | Need Help?? |
A short while back I was called in to look at some Perl code that didn't seem to be working right. The script in question had originally been supplied by a vendor, then gotten hacked to pieces by various people in my group, who are by and large Java programmers. Which might explain the following chunk of code I discovered:
Since I now spend most of my time in Java as well, it took me a couple of seconds to realize the common idiom variable != null is completely inappropriate in Perl. Without strictures, perl was parsing the condition as $param "not equal to" the string 'null'. Certainly not the intended semantics. Still, it seemed to me that Perl was conveniently Doing What I/We Meant. If foo was passed in (and its value would almost certainly never be 'null'), then $param didn't equal 'null', and the conditional block would execute. Our problem was that the conditional block was not executing. What was going on here? Later on the drive home, the light bulb came on. The condition was doing a numerical comparison (!=), not a string comparison! So: Given that $param only contained straight text (no digits), its numerical value would always be 0. Hence the conditional would never execute. Anyway, I got a kick out of this. Perl's DWIMmery can be a powerful tool, but also a dangerous hazard in the wrong hands. Swiss Army Chainsaw indeed -- just make sure you stand clear!
Back to
Meditations
|
|