morgon has asked for the wisdom of the Perl Monks concerning the following question:
I am currently looking for "near miss" examples in Perl.
More specifically I am looking for innocently looking code-examples that are almost but not quite correct and fail spectaculary.
Maybe an example clarifies what I am looking for:
Imagine this piece code:
This may be not an idiomatic way to loop but it is semantically ok: @a-1 (in scalar context) is the index of the last element in @a;use strict; my @a=(1..100); for my $i (0..@a-1) { print $a[i]; }
Now imagine someone refactors this to use an array-ref instead of an array like this:
This looks similar to the above but is now only "almost" correct - the problem is the expression $a-1. Unfortunately a array-ref in numeric context returns the memory-location (usually a LARGE number) and so this code will probably loop for a very long time (and/or run out of memory eventually). And in case you ask: No, I am not making this up, I have seen it happen...use strict; my $a=[1..100]; for my $i (0..$a-1) { print $a->[i]; }
Anyone with similar examples?
Many thanks!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: catastrophic near misses in Perl
by repellent (Priest) on Apr 18, 2009 at 02:19 UTC | |
|
Re: catastrophic near misses in Perl
by CountZero (Bishop) on Apr 18, 2009 at 07:08 UTC | |
|
Re: catastrophic near misses in Perl
by moritz (Cardinal) on Apr 18, 2009 at 09:02 UTC | |
by Marshall (Canon) on Apr 18, 2009 at 10:15 UTC | |
|
Re: catastrophic near misses in Perl
by ruzam (Curate) on Apr 18, 2009 at 16:08 UTC | |
|
Re: catastrophic near misses in Perl
by dsheroh (Monsignor) on Apr 18, 2009 at 11:22 UTC | |
by Anonymous Monk on Apr 18, 2009 at 11:42 UTC | |
|
Re: catastrophic near misses in Perl
by Marshall (Canon) on Apr 18, 2009 at 09:31 UTC | |
by FunkyMonk (Bishop) on Apr 18, 2009 at 10:58 UTC |