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!
In reply to catastrophic near misses in Perl by morgon
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |