Mailing List Archive

2 bugs for the price of one
consider this code:

1 #!/usr/bin/perl
2 # test bugger
3
4 $x = 0;
5
6 $i++;
7
8 for ($i = 0; $i/$j < 100; $i++) {
9 $k = 0;
10 last if $i/0;
11 $k++;
12 }
13 print "darn";

If you run this, the div-by-zero problem is reported at line 12 instead of line 8.
Also, if you run it in the debugger, the debugger fails to catch the exception
and bails out unexpectedly.

--tom
Re: 2 bugs for the price of one [ In reply to ]
> If you run this, the div-by-zero problem is reported at line 12 instead
> of line 8.

`for' needs in general to be fixed up.

Here is a more serious `for + lexical' bug, reported at 2 May 1995:

#!/local/perl5/bin/perl -wl
$x = "a";
{
for (my $x = 0; $x < 3; $x++) {
print $x; # prints a, 1, 2
}
print $x; # prints 0
}
print $x; # prints 3

Output should be 0, 1, 2, 3, a.

It warns `Argument "a" isn't numeric for numeric lt at a.pl line 6.'


Regards,

Hallvard
Re: 2 bugs for the price of one [ In reply to ]
> `for' needs in general to be fixed up.


Here's another one. Larry, if it's feasible, I would like to request a
warning from the compiler when someone writes:

for ($i = 0, $i < 10, $i++) {

}


When they surely meant:

for ($i = 0; $i < 10; $i++) {

}


rather than:

foreach $_ ( ($i = 0), ($i < 10), ($i++) ) {

}

--tom