[Israel.pm] catching $a, $b unnecessary my and maybe other things
Gabor Szabo
gabor at perl.org.il
Sun Jun 6 11:37:22 PDT 2004
There are two issues that might or might not relate to each other.
Look at this code:
#!/usr/bin/perl
use warnings "all";
use strict;
$a = 'Because of their special use by sort(), ';
$a .= 'the variables $a and $b are exempted from "strict vars" check.';
f(1);
f(-1);
sub f {
my $x = shift;
my $y = "unknown";
if ($x < 0) {
$y = "negative";
} elsif ($x > 0) {
my $y = "positive";
}
return $y;
}
There are two issues here:
1)
use strict; (more precisely use strict "vars") does not care about
$a and $b. This issue is documented in perldoc strict but it still
caused me problems by not shouting. I guess it would be hard to see if
a certain occurance of $a and $b was part of a sort function or not
but there might be some flag (and option to use warnings) that will
catch all occurances of $a and $b
(I know grep can help in this but you have to actually remember to use it.)
2)
my $y = "positive";
within the elsif is actually a bug. In this case it is quite obvious
to a reader that the $y within this block is used only once. Can such
thing be caught programmaticaly ?
3)
There might be other small thing, just these two came up recently.
Gabor
More information about the Perl
mailing list