Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( [id://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

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (7)
As of 2024-03-29 08:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found