It's amazing how something so minor can turn Perl from a happy, simple programming language into an evil cancerous growth that consumes all available memory on your system. Not even strict or -w could save you.

It all started with some code that used used to extract a particular value from a simple HoH:
my $foo = { foo => { bar => 'baz' } }; foreach (keys %foo) { if (defined($foo->{$_}->{foo}) && defined($foo->{$_}->{foo}->{bar})) { return $foo->{$_}->{foo}->{bar}; } return; }
To prevent auto-vivification, it tests carefully and returns the appropriate value. This worked well in practice, but in order to make the function more useful, it now had to search an AoHoH and return the first match. A simple change, of course. How about this:
my $foo = [ { foo => { bar => 'baz' } }, ]; foreach my $foo_entry (@$foo) { foreach (keys %$foo_entry) { if (defined($foo->{$_}->{foo}) && defined($foo->{$_}->{foo}->{bar})) { return $foo->{$_}->{foo}->{bar}; } } return; }
Can you spot the error? strict can't. When I run this, I'm into swap space in a matter of seconds, and...then...things...get...very...slow.

Somehow I messed up the variable names, since in the actual source they were slightly more similar and there was more clutter which prevented me from seeing this right away.

If the code is "fixed", it runs fine.

What is strange is that this is all caused by using a AoHoH instead of a AoH. An example using just AoH causes a warning:
my $foo = [ { foo => 'bar' }, ]; foreach my $foo_entry (@$foo) { foreach (keys %$foo_entry) { if (defined($foo->{$_}->{foo}) && defined($foo->{$_}->{foo}->{bar})) { return $foo->{$_}->{foo}->{bar}; } } return; }
Something is falling through the cracks here, no?

In reply to Automagic Array to Hash Conversion? Pseudo-Hash Explosion by tadman

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.