logo       

Re: On using $_ in subroutines: msg#00409

perl-beginners

Subject: Re: On using $_ in subroutines


> 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.
>
> local() only works on variables that are in the symbol table, in other
> words package variables. All variables that are a single puntuation
> character are package variables and some are global and effect all
> packages. local() does not create a variable it just masks the current
> value of a variable within the current scope.
>
> my() creates variables that are not in the symbol table and are visible
> only within the current block or file.


Ah, that cleared it up. Thanks, John.

- Bryan




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


<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | Mail Home | sitemap | FAQ | advertise