-
Can you spot the bug?
2005-02-03 13:49 in /tech/perl/HallOfShame
Can you spot the bug in this code?
use strict; use constants { STATE_A => 0, STATE_B => 1, STATE_C => 2, }; ... if ($state == STATE_A) { do_a(); } elsif ($state == STATE_B) { do_b(); } elsif ($state == STATE_C) { do_c(); } else { die("bad state: $state"); }Okay, actually there are a couple bugs:
- The code doesn’t
use warnings. This might not seem like a bug to some except... - Perl numifies
undefto 0, so the first condition passes when$stateis undefined, instead of dying. And, since you weren’t using warnings it’s not clear that this code is the culprit when other code starts subtly misbehaving.
So people don’t think I only pick on other people’s code, I’ll disclose that I wrote the state-handling code. However, I was not the person who failed to turn on warnings.
- The code doesn’t
Leave a comment
Please use plain text only. No HTML tags are allowed.
Comments are closed for this story.
Trackbacks are closed for this story.