Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
"Please oh please don't require people to search the entire code of your routine in order to figure out what parameters can be passed to it!"

Yes, it wouldn't make sense to use %_ in most long & complicated routines.

I never intended to suggest that it would become The One True WayTM for accessing subroutine arguments.

I just think it would be a useful additional tool in the Perl developer's toolbox, nicely complementing the existing ones (in particular @_ which it would semantically mirror very closely), to be deployed with discretion.

I myself would use %_ sparingly, just like I currently use direct access to $_[0] (or, similarly, $_ in foreach loops) sparingly. But sparingly does not mean "never useful"!
Remember that Perl is also popular for shell one-liners & small system administration/convenience scripts etc., not just for huge professional web applications. And not every subroutine is so big and complicated, that direct usage of @_ or %_ would get lost in the noise or make it impossible to predict unwanted side-effects.

Example:

In the OP I've already given a use-case that can appear even in big Perl applications, where I think %_ has its place - let me elaborate on that:
When you have, say, a module which offers functionality for which it takes a whole set of related callback functions, which when called should get access to a set of variables (but as the module author you can't predict which variables will actually be used by which user callback), then passing a hash makes sense. Now if, in the user code, the callbacks turn out to be very simple and short (say, <1 line each), then copy&pasting the same boilerplate between all of them for unpacking the parameters into a hash just adds noise - "less sexy" in this case also means "less readable".

Precedent:

Besides @_, the use of %_ as I imagine it can be compared to the use of $_ in foreach loops.
The "safe", "clean", "text-book" way is to declare an explicit loop variable, and in most cases that does indeed makes sense:

foreach my $person (@people) { ## ... ## Long and complex code that uses $person many times, might ## potentially be refactored into nested loops in the future, etc. ## ... }

Using $_ instead of $person in that example would entail many of the same or similar disadvantages/problems as the ones that you and other commenters have invoked against the use of %_ in subs.

And yet, Perl does make $_ available as a built-in language feature.
And I, for one, happily use it to make code shorter and cleaner in those cases where it doesn't hurt, e.g.:

say $_->{name} foreach @people;
$age_sum += $_->{age} foreach @people;

Perl trusts me to make an informed decision when to use $_ and when not. The fact that it can be used in ways that make code unreadable or buggy, wasn't deemed enough of a reason to exclude it from the language.

Why not apply the same principle to %_?


In reply to Re^2: Why doesn't Perl provide %_ as the hash equivalent of @_ in subs? (ugly++) by smls
in thread 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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (5)
    As of 2014-09-20 01:57 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (151 votes), past polls