cbeckley has asked for the wisdom of the Perl Monks concerning the following question:
One of the answers to Why does ‘keys’ need a named hash? made me remember a question that's been bugging me for awhile.
duelafn replied with
return keys %{ {map { $_ => 1 } @_} };
I've been using this syntax and have accepted it as idiomatic Perl, however, what I don't get is, why the curly braces? And why two pairs of them? Is there a reference dereference happening?
Given this:
I get:say Dumper(\%{ {map { $_ => 'fish' } qw(one two red blue)} } ); ^ ^ ^ ^ |-|-----------------------------------------|-|
If I remove the inner pair of indicated braces, I get$VAR1 = { 'blue' => 'fish', 'one' => 'fish', 'red' => 'fish', 'two' => 'fish' };
Ambiguous use of %{map{...}} resolved to %map{...}
So the inner pair is a code block. I'm not sure that clears anything up for me, unless I'm being thick ...
If I need to return an array, as opposed to a list or a hash, what would that syntax be?
So if I want this:
How do I do it?$VAR1 = [ 'one', 'fish', 'two', 'fish', 'red', 'fish', 'blue', 'fish' ];
producessay Dumper(\@{ {map { $_ => 'fish' } qw(one two red blue)} } );
Which implies this is not a dereferencing thing ... unless of course I'm doing it wrong.Not an ARRAY reference
Thanks,
cbeckley
|
---|
Back to
Seekers of Perl Wisdom