Home > Archive > PERL Miscellaneous > January 2008 > Relying on $_
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Bernie Cosell 2008-01-24, 7:18 pm |
| As a matter of style and expectation, I'm wondering about relying on $_.
Often, just for convenience I'll do a no-var foreach. For example:
foreach (sort keys %SOMEHASH)
{ print "Processing $_\n" ;
my $target = $SOMEHASH{$_} ;
....
}
and I'll refer to $_ here and there in the loop [e.g., if something fails
I'll typically just do a 'die "Error processing $_: $!\n" ;' or the like].
Is that bad form? I just sorted out a minor problem where I was calling an
object method and I was surprised to discover that $_ was clobbered when I
got back from the method.
Is this a bug [even if a minor one] in the package? If not, what are the
rules for when you should and shouldn't expect $_ to get messed up?
/Bernie\
--
Bernie Cosell Fantasy Farm Fibers
bernie@fantasyfarm.com Pearisburg, VA
--> Too many people, too few sheep <--
| |
| smallpond 2008-01-24, 7:19 pm |
| On Jan 24, 12:15 pm, Bernie Cosell <ber...@fantasyfarm.com> wrote:
> As a matter of style and expectation, I'm wondering about relying on $_.
> Often, just for convenience I'll do a no-var foreach. For example:
> foreach (sort keys %SOMEHASH)
> { print "Processing $_\n" ;
> my $target = $SOMEHASH{$_} ;
> ....
> }
> and I'll refer to $_ here and there in the loop [e.g., if something fails
> I'll typically just do a 'die "Error processing $_: $!\n" ;' or the like].
> Is that bad form? I just sorted out a minor problem where I was calling an
> object method and I was surprised to discover that $_ was clobbered when I
> got back from the method.
>
> Is this a bug [even if a minor one] in the package? If not, what are the
> rules for when you should and shouldn't expect $_ to get messed up?
>
It is almost never a good idea to use $_ (or $a or $b) as
the loop variable in a foreach. see perltrap and perlvar.
"my $key" is quite reasonably priced and doesn't
get clobbered by subs that you call.
The rule is pretty simple: there's only one $_ in your
package, and there are many functions that use it implicitly.
Many subs should have local $_ but don't, so it is never good
to assume it will be preserved across a call.
The other thing to remember about foreach is that it creates an
alias for the value, not a copy.
| |
| Michele Dondi 2008-01-25, 8:14 am |
| On Thu, 24 Jan 2008 12:15:57 -0500, Bernie Cosell
<bernie@fantasyfarm.com> wrote:
>As a matter of style and expectation, I'm wondering about relying on $_.
>Often, just for convenience I'll do a no-var foreach. For example:
> foreach (sort keys %SOMEHASH)
> { print "Processing $_\n" ;
> my $target = $SOMEHASH{$_} ;
> ....
> }
>and I'll refer to $_ here and there in the loop [e.g., if something fails
>I'll typically just do a 'die "Error processing $_: $!\n" ;' or the like].
>Is that bad form? I just sorted out a minor problem where I was calling an
IMHO, it's all a matter of how long the actual for block is. As far as
I'm concerned, I use $_ if it is *at most* say ten lines of code or
so.
>object method and I was surprised to discover that $_ was clobbered when I
>got back from the method.
Then that method was doing something very Bad(TM) i.e. manipulating $_
directly.
Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{po
p^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
| |
| Ted Zlatanov 2008-01-25, 7:14 pm |
| On Thu, 24 Jan 2008 12:15:57 -0500 Bernie Cosell <bernie@fantasyfarm.com> wrote:
BC> As a matter of style and expectation, I'm wondering about relying on $_.
BC> Often, just for convenience I'll do a no-var foreach. For example:
BC> foreach (sort keys %SOMEHASH)
BC> { print "Processing $_\n" ;
BC> my $target = $SOMEHASH{$_} ;
BC> ....
BC> }
BC> and I'll refer to $_ here and there in the loop [e.g., if something fails
BC> I'll typically just do a 'die "Error processing $_: $!\n" ;' or the like].
BC> Is that bad form? I just sorted out a minor problem where I was calling an
BC> object method and I was surprised to discover that $_ was clobbered when I
BC> got back from the method.
BC> Is this a bug [even if a minor one] in the package? If not, what are the
BC> rules for when you should and shouldn't expect $_ to get messed up?
I'd say if there's more than two lines of code in the loop code, don't
use $_. It is very handy for map/grep/postfix-foreach loops but can
make life hell otherwise.
Ted
| |
| Ben Morrow 2008-01-25, 7:14 pm |
|
Quoth Ted Zlatanov <tzz@lifelogs.com>:
> On Thu, 24 Jan 2008 12:15:57 -0500 Bernie Cosell
> <bernie@fantasyfarm.com> wrote:
>
> BC> As a matter of style and expectation, I'm wondering about relying on $_.
> BC> Often, just for convenience I'll do a no-var foreach. For example:
>
> BC> Is this a bug [even if a minor one] in the package? If not, what are the
> BC> rules for when you should and shouldn't expect $_ to get messed up?
>
> I'd say if there's more than two lines of code in the loop code, don't
> use $_. It is very handy for map/grep/postfix-foreach loops but can
> make life hell otherwise.
This is probably an appropriate time to point out 5.10 has a lexical $_:
~% perl5.10.0 -E'
sub foo { say shift; $_ = 1; }
my $_; foo $_ for 2, 3, 4;
'
2
3
4
Ben
| |
|
| Jennifer Aniston and Jessica Alba , Movies Of Crucified Lesbian Slave!
[url]http://www.BestG Blog.com/a?id=726648[/url] |
|
|
|
|