APM: bugs
Mike Stok
mike at stok.co.uk
Mon Dec 2 10:50:40 CST 2002
On Mon, 2 Dec 2002, Wayne Walker wrote:
> In a sideline discussion, someone had been bitten by something like:
>
> if ($foo = -1) { sub1; action2}
>
> The bug is a missing = (= vs ==).
>
> A friend of mine has given great advice, that I haven't implemented very
> well yet. He suggests that everytime you compare a scalar and a
> constant put the constant on the left side:
>
> if (-1 == $foo) { sub1; action2}
>
> That way, if you do forget the second = sign:
>
> if (-1 == $foo) { sub1; action2}
>
> then you get a compile time error:
>
> "Can't modify constant item in scalar assignment"
If you use -w or warnings then you can get a warning without having to go
through mental gymnastics (the if constant equals a variable seems stilted
to me).
[mike at won tmp]$ cat try.pl
#!/usr/bin/env perl
use warnings;
use strict;
my $x = 1;
if ($x = 2) {
print "oops\n";
}
[mike at won tmp]$ perl try.pl
Found = in conditional, should be == at try.pl line 8.
oops
Of course warnings (and maybe strict) help with other stuff too.
> Once you get in the habit of putting the constant first, this very hard
> to catch bug screams "Fix me" as soon as you make the mistake.
>
> Of course, unfortunately, it does not help at all when comparing to
> variables.
Indeed.
Mike
--
mike at stok.co.uk | The "`Stok' disclaimers" apply.
http://www.stok.co.uk/~mike/ | GPG PGP Key 1024D/059913DA
mike at exegenix.com | Fingerprint 0570 71CD 6790 7C28 3D60
http://www.exegenix.com/ | 75D2 9EC4 C1C0 0599 13DA
More information about the Austin
mailing list