We don't bite newbies here... much | |
PerlMonks |
'each' syntax oddity?by SuperCruncher (Pilgrim) |
on Mar 07, 2002 at 13:07 UTC ( [id://150000]=perlquestion: print w/replies, xml ) | Need Help?? |
SuperCruncher has asked for the wisdom of the Perl Monks concerning the following question:
Last night I was experimenting with XML::Parser, trying to parse an XML document. I encountered what I consider to be a pretty strange syntax/context oddity.
produces the error message: Type of arg 1 to each must be hash (not private array) at test.pl line 2, near "@array) " Peculiar, I thought--why can't the array be "automagically" converted to a hash? But anyway.... I know I can create a hash by assigning an array to a hash, and although each requires a HASH, I thought any expression that evaluates to a hash would be ok. So I try the following code: I expected that to work: surely the my %hash = @array code is evaluated first, resulting in a hash. But this code didn't work either, I got the error: Type of arg 1 to each must be hash (not list assignment) at test.pl line 2, near "@array) " So I decided to insert another line before the while statement, as shown below: This did finally have the desired effect, but it seems incredibly redundant and un-Perl-like to have to have the array to hash assignment as a separate statement. Obviously if I want to create a hash, perl needs to create a hash table to do the look-ups so hashes aren't identical to arrays, but it seems strange that it can't automatically do this, especially when many Perl operators and functions act in magical ways (e.g. the ++ operator on strings).
Back to
Seekers of Perl Wisdom
|
|