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
work, butsub uniq { my %hash = map { $_ => 1 } @_; return keys %hash; }
not?sub uniq { return keys map { $_ => 1 } @_; }
Edit:
I express my sincerest thanks to the monks for a warm welcome into the monastery.
Following the suggestions, I tried
which works perfectly fine.sub uniq { # two pairs of braces around map return keys %{ { map { $_ => 1 } @_ } }; }
However, prior to this, even before I posted the question, I experimented with
and it did not work.sub uniq { # single pair of braces around map return keys %{ map { $_ => 1 } @_ }; }
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?