Venerable Monks,

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:

use strict; my @a=(1..100); for my $i (0..@a-1) { print $a[i]; }
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;

Now imagine someone refactors this to use an array-ref instead of an array like this:

use strict; my $a=[1..100]; for my $i (0..$a-1) { print $a->[i]; }
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...

Anyone with similar examples?

Many thanks!


In reply to catastrophic near misses in Perl by morgon

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.