On 12 March 2010 09:17, Uri Guttman <[email protected]> wrote:
>>>>>> "PP" == Philip Potter <[email protected]> writes:
>
> PP> I found this code example on Stack Overflow to prettyprint a hash:
> PP> (link:
> PP>
> http://stackoverflow.com/questions/2431032/how-to-print-a-key-value-using-the-perl-hases/2431139#2431139
> PP> )
>
> you shouldn't be learning perl from that site!
I respectfully disagree.
> PP> my %hash = (2009 => 9, 2010 => 10);
>
> see what happens when you have 3 or more pairs in that hash!
>
> PP> print join (" => ", each %hash), "\n" while <keys %hash>;
>
> try doing a perl one liner with -MO=Deparse and see what perl parses
> that as.
Great idea, although I don't see the need to do it as a one liner:
> perl -MO=Deparse foo.pl
use warnings;
use strict 'refs';
my(%hash) = (2009, 9, 2010, 10);
use File::Glob ();
print join(' => ', each %hash), "\n" while defined($_ = glob('keys %hash'));
foo.pl syntax OK
> perl -E 'say while <keys %hash>'
keys
%hash
So keys %hash is being treated as a string, not an expression, the
keys function never gets called, and the loop executes twice, once
with $_ = 'keys' and once with $_ = '%hash'. Truly horrible!
Phil
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/