Relying on $_

Relying on $_

am 24.01.2008 18:15:57 von Bernie Cosell

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

Re: Relying on $_

am 24.01.2008 18:58:50 von smallpond

On Jan 24, 12:15 pm, Bernie Cosell 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.

Re: Relying on $_

am 25.01.2008 12:03:49 von Michele Dondi

On Thu, 24 Jan 2008 12:15:57 -0500, Bernie Cosell
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{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^ ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,

Re: Relying on $_

am 25.01.2008 19:22:27 von Ted Zlatanov

On Thu, 24 Jan 2008 12:15:57 -0500 Bernie Cosell 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

Re: Relying on $_

am 25.01.2008 23:40:36 von Ben Morrow

Quoth Ted Zlatanov :
> On Thu, 24 Jan 2008 12:15:57 -0500 Bernie Cosell
> 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