Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Comment on

( #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

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
    LanX ♪..♫ Fiesta, fiesta mexicana ... ♪..♫

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (11)
    As of 2018-06-18 17:36 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (110 votes). Check out past polls.