in reply to Automagic Array to Hash Conversion? Pseudo-Hash Explosion

I agree with Alf; it is a bug in Perl's pseudohash support. You happen to have an arrayref of which the first element is a hashref, and that is the definition of a pseudohash. This triggers the same all-you-can-eat memory bug:

my $foo; $foo->[0]->{foo}->{bar} = 'baz'; $foo->{foo}->{foo}->{bar} = 'baz';

As ferrency points out, your examples are confusing. I suspect that something was lost when you trimmed them for posting. Even the first example, which (I think) you meant to be correct, fails on the data you posted. Either $foo needs an additional level of hashref (becoming HoHoH), or the foreach loop is not needed. Also, your return statements should be one level further out in all three examples.

I was working on an example of a less error-prone way to handle your AoHoH, when I realized that something was amiss in the example code. Can you clarify?

Replies are listed 'Best First'.
Re^2: Automagic Array to Hash Conversion? Pseudo-Hash Explosion
by tadman (Prior) on May 14, 2002 at 20:04 UTC
    The second example is the one that crashes Perl for me. It looks a little strange, I'll admit, because it's supposed to be inside a function, and $foo is supposed to be passed in via @_. Here's an idea of how it looked:
    sub bar { my ($foo) = @_; foreach my $foo_entry (@$foo) { foreach (keys %$foo_entry) { if (defined($foo->{$_}->{foo}) && defined($foo->{$_}->{foo}->{bar})) { return $foo->{$_}->{foo}->{bar}; } } } return; }
    During simplification, I accidentally put the return inside the foreach, but it doesn't matter. The defined call kills it right away.

    This code is busted, as I forgot to change the internal references to $foo_entry. What surprised me was that a) strict didn't care, and b) it caused Perl to go crazy.

    After some experimentation, I came up with that example as the least amount of code required to reproduce the problem. The data given is complete junk, the real data much more complex, but it too is the bare minimum required to produce the problem. The real structure does have additional levels of "oH".

    Pseudo-hashes are dangerous to your health then?