Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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.

In reply to Re: Perl Idioms Explained - my ($foo, $bar) = @{shift(@_)}{qw/ -foo -bar /} by Aristotle
in thread Perl Idioms Explained - my ($foo, $bar) = @{shift(@_)}{qw/ -foo -bar /} by Roger

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (8)
    As of 2020-09-29 16:55 GMT
    Find Nodes?
      Voting Booth?
      If at first I donít succeed, I Ö

      Results (148 votes). Check out past polls.