Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: a rule of thumb for $_ in Perl

by TGI (Parson)
on Oct 02, 2007 at 20:02 UTC ( [id://642228]=note: print w/replies, xml ) Need Help??


in reply to a rule of thumb for $_ in Perl

I agree with you general sentiment. IMO $_ should be used to increase readability. For the most part explicit use of $_ is a detriment to readability.

For example, I will use $_ explicitly when I would otherwise be creating a throwaway variable:

foreach ( @array_of_array_refs ) { my @current_array = @$_; # do some destructive processing on @current_array } #compare to foreach my $current_array_ref ( @array_of_array_refs ) { my @current_array = @$current_array_ref; # do some destructive processing on @current_array }

I feel believe that this helps reduce visual noise, and keeps the code focused on what is important. Unpacking $_ to a scalar may be used to avoid side effects due to the aliasing of loop variables.


TGI says moo

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://642228]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2024-03-29 14:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found