cholt has asked for the wisdom of the Perl Monks concerning the following question:

Does anyone know why this happens?

In perl 5.8.8 I came across a bug after much trouble with some code.

If I have the following data structure:

 $test = [{augustus => ''}];

and then do the following:

each %{$test};

I get the error "Bad index while coercing array into hash", which is correct. However if I have the slightly modified data structure:

$test = [{augustus => []}];

Then this works:

each %{$test};

It produces no error, and can result in a huge weird array that fills up large chunks of memory, and is difficult to find when debugging.

I have verified this on 5.8.8 and 5.8.9 on both Linux and Mac OSX.

Perl 5.10.0 at least throws a "Not a HASH reference" error.

Replies are listed 'Best First'.
Re: can coerce array into hash
by JavaFan (Canon) on Dec 01, 2010 at 21:22 UTC
    Perl's object system sucks. So, once upon a time, some people thought that "pseudo-hashes" were the solution, and "pseudo-hashes" became part of the core. It soon dawned upon people that Perl's object system sucks, but that "pseudo-hashes" sucked more. Given Perl's "let's be very very careful with removing functionality" policy, and the long reign of 5.8.x, the last remnants of "pseudo-hashes" did not disappear before 5.10.

    Pseudo-hashes were implemented as arrays whose first element was a reference to a hash.

Re: can coerce array into hash
by ikegami (Patriarch) on Dec 01, 2010 at 19:52 UTC
    According to what you said, it's a bug fixed in 5.10.