Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

TL;DR version:

I think %_ should be available inside subroutines, and contain the same parameter list as @_ but interpreted as a hash.
Why is this currently not the case?

Longer version:

For functions that needs to handle a flexible number of named arguments, one would traditionally pass a hash and then add a line at the top of the function to rebuild the hash from @_:

sub dostuff { my %arg = @_; # Access named parameters like $arg{foo} } dostuff( foo => 'FOO', bar => 1 );

If it's a long and unique function, that's fine because the syntactical overhead is negligible. However, consider cases where you have multiple small (say, one-liner) functions that are all called the same way - for example when using lambda functions as callbacks:

sub_with_callbacks ( red => sub { my %arg = @_; .. $arg{foo} .. }, green => sub { my %arg = @_; .. $arg{foo} .. }, blue => sub { my %arg = @_; .. $arg{foo} .. }, yellow => sub { my %arg = @_; .. $arg{foo} .. } );

The my %arg = @_; has now become annoying boilerplate that makes my code less sexy. I hate that.

Compared to other languages, Perl usually excels at providing little tricks and magic variables and whatnot to allow me to create cute and condensed snippets of code in cases where that is appropriate. But here, the (to my mind) obvious feature is missing: Allowing direct access to the parameter list (interpreted as a hash) via %_ - which would make it possible to replace the previous example code block with the following, much nicer one:

sub_with_callbacks ( red => sub { .. $_{foo} .. }, green => sub { .. $_{foo} .. }, blue => sub { .. $_{foo} .. }, yellow => sub { .. $_{foo} .. } );

I wonder why Perl does not provide this feature, seeing as:

  • It fits semantically: $_[0] accesses positional parameters, $_{foo} would access named parameters.
  • To me, it just feels like the obvious thing to do with %_. Surely I can't be the only one?
  • %_ already counts as a reserved variable name (despite not being used), so it wouldn't break any backwards compatibility to add it.
  • As for performance impact, I'm sure the devs could make it so %_ is only created for functions that actually use it, and/or implement it as a tied hash or some other magic.

I'd be interested in hearing any qw(thoughts opinions explanations background-info musings) that my fellow Monastery dwellers might have on this.


In reply to Why doesn't Perl provide %_ as the hash equivalent of @_ in subs? by smls

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 exploiting the Monastery: (3)
As of 2024-04-20 02:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found