[Israel.pm] Perl vs. Ruby on Two Idioms

Jason Elbaum jason.elbaum at gmail.com
Sun Jun 22 02:18:34 PDT 2008

> one would need at least several days of specific Perl experience
> to understand what's going on here:
> my %h = (map { $_ => 1 } @a);

I agree. (And "days" is generous on your part.) While this may not
qualify as obfuscated Perl, and it is indeed not an uncommon idiom, it
requires a non-trivial amount of Perl knowledge to comprehend. Whether
or not it is appropriate in a given context would depend on the level
of Perl experience expected of the developers. I wouldn't expect a
beginner to understand this, nor would I want him to have to waste his
time doing so.

Incidentally, the parentheses in the above code are redundant and
confusing, as they lead one to assume incorrectly that they are
required for list context. The correct code should be:

my %h = map { $_ => 1 } @a;

> The direct Perl translation of my last solution is:
> my %h; for my $i (@a) { $h{$i} = 1; }

I would likely write it like this, since I find the loop variable to
be overkill in a one-line context:

my %h;
$h{$_} = 1 for @a;

But your version is fine too, and I agree that it should generally be
preferred to Shlomi's.


Jason Elbaum

More information about the Perl mailing list