http://qs321.pair.com?node_id=1187094

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

The perldoc on keys clearly states

Called in list context, returns a list consisting of all the keys of the named hash…
but makes no attempt to explain why.

Googling around for an explanation also yielded no results.

Put very simply, why does

sub uniq { my %hash = map { $_ => 1 } @_; return keys %hash; }
work, but
sub uniq { return keys map { $_ => 1 } @_; }
not?


Edit:

I express my sincerest thanks to the monks for a warm welcome into the monastery.

Following the suggestions, I tried

sub uniq { # two pairs of braces around map return keys %{ { map { $_ => 1 } @_ } }; }
which works perfectly fine.

However, prior to this, even before I posted the question, I experimented with

sub uniq { # single pair of braces around map return keys %{ map { $_ => 1 } @_ }; }
and it did not work.

I had always believed that braces can convert a list with even number of elements into a valid HASHref, and modulo (%) can convert any valid HASHref into a hash.

The question now is this: if map just produces a list, why does it need two pairs of braces to emit a valid HASHref?