in reply to Explanation of commonly used Perl

Unroll it, bit by bit:

@_We'll take the parameters passed to the sub..
{@_}..and make an anonymous hash out of them.
{{@_}}{ ... }..now we're going to dereference this hashref..
@{{@_}}{qw/ ... /}..but take a slice of it when we do so;
@{{@_}}{qw/foo bar/}Specifically, we'll extract the foo and bar values from the hashref..
my ($foo, $bar) = @{{@_}}{qw/foo bar/}..and put them into $foo and $bar

Essentially, the code is equivilent to the following, but avoids the %args variable:

sub foobar { my %args = @_; my $foo = $args{foo}; my $bar = $args{bar}; }

perl -pe '"I lo*`+$^X$\"$]!$/"=~m%(.*)%s;$_=$1;y^`+*^e v^#$&V"+@( NO CARRIER'

Replies are listed 'Best First'.
Re: Re: Explanation of commonly used Perl
by perrin (Chancellor) on Apr 16, 2002 at 22:25 UTC
    In my opinion, your translation is a whole lot easier to read and probably a better idea for maintainable code. The original is pretty obfuscated and would definitely confuse a beginner.

      I would agree. The only "advantage" to doing the hashref / dereference / slice trick is that one avoids the %args hash; frankly, I don't consider that an advantage at all. Most of the time when I pass args to a function as a hash, I simply use the %args hash in the function, instead of pulling specific values out and stuffing them into scalars. I like to think that this makes for better maintainability and scalability.

      But to each their own..

      perl -pe '"I lo*`+$^X$\"$]!$/"=~m%(.*)%s;$_=$1;y^`+*^e v^#$&V"+@( NO CARRIER'