http://www.perlmonks.org?node_id=333038


in reply to Perl Idioms Explained - my ($foo, $bar) = @{shift(@_)}{qw/ -foo -bar /}

I have to agree with the others — this is far too convoluted and too seldomly useful to be a reasonable idiom. If you must, I'd at least replace the shift with a simple $_[0] so what's going on is just a little more obvious. I still don't like it one bit, though. If you don't have any positional parameters, why are you passing an anonymous hash? If you don't, it's easy to make the happenings blindingly obvious at the cost of one extra line:

sub foobar { my %arg = @_; my ($foo, $bar) = @arg{qw/ -foo -bar /}; # .. }

Even if you have positional parameters, an easy solution is to put them at the beginning of the list so that you can say something like

my ($foo, $bar, %arg) = @_; my ($baz, $quux) = @arg{qw(baz quux)};

The one case a hashref makes sense is in case of a method called on a hash-based class.

But then your "idiom" means throwing away $self, which I'd consider a bad idea. Even if the particular method's code is sufficiently simple to allow for this, it makes a future change in requirements that requires a calling a method on $self more difficult.

More importantly, I use my $self = shift; as a form of self documentation in the code. Helper non-method functions included in a package do not have this, so you can see at a glance what is a function and what is a method. And in that case this "idiom" is completely redundant:

sub moo { my $self = shift; my ($foo, $bar) = @$self{qw(foo bar)}; # .. }

Makeshifts last the longest.

Replies are listed 'Best First'.
Re: Re: Perl Idioms Explained - my ($foo, $bar) = @{shift(@_)}{qw/ -foo -bar /}
by Roger (Parson) on Mar 01, 2004 at 22:43 UTC
    I would agree with you that this is not the best approach and I don't use it in my code anyway. Yesterday someone asked in the CB about what the following code is doing:
    my ($foo, $bar) = @{+shift}{qw/ -foo -bar/};

    I thought it might be a good idea to post it. It's not that obvious to the less experienced coders that the hash slice @values = @hash{@keys} is involved. There was a supersticious '+' in the original question as well, which I have removed.