|Just another Perl shrine|
Why doesn't Perl provide %_ as the hash equivalent of @_ in subs?by smls (Friar)
|on Sep 26, 2013 at 09:47 UTC||Need Help??|
I think %_ should be available inside subroutines, and contain the same parameter list as @_ but interpreted as a hash.
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 @_:
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:
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:
I wonder why Perl does not provide this feature, seeing as:
I'd be interested in hearing any qw(thoughts opinions explanations background-info musings) that my fellow Monastery dwellers might have on this.