On Tuesday 28 July 2009 20:06:34 Bryan R Harris wrote:
> > Bryan Harris wrote:
> >> John W. Krahn wrote:
> >>> Bryan Harris wrote:
> >>>> ... but by modifying $_ I was clobbering $_ elsewhere in the larger
> >>>> program!
> >>>
> >>> Yes because $_ is a special global variable.  This effect is called
> >>> "action at a distance" which is why it is better to use named lexically
> >>> scoped variables instead of $_.
> >>
> >> I have the Perl Bookshelf on CD (and perldoc, obviously) -- where can I
> >> read more about this?
> >
> > perldoc perlvar
> >
> > http://shoebox.net/articles/perl-warts.html
> > http://en.wikipedia.org/wiki/Action_at_a_distance_(computer_science)
>
> Great info, thanks.
>
> Curiously, perlvar seems to recommend localizing $_ with "local", but I
> thought I read somewhere to never use local and only use my.  I still don't
> understand how those two are different.
>

Well, "my $_" is only possible starting from perl-5.10.0. Not below it. If you 
want your scripts to be compatible with previous version of perl5 (including 
5.8.x) you'll need to use "local $_".

local $_ is usually good enough from my experience. I should note that I try 
to avoid using $_ as much as possible in any half-serious code, because 
relying on it is prone to errors because so many Perl built-ins modify it.

Regards,

        Shlomi Fish

-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
Optimizing Code for Speed - http://xrl.us/begfgk

God gave us two eyes and ten fingers so we will type five times as much as we
read.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to