logo       

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

perl-beginners

Subject: Re: On using $_ in subroutines



>> For example, my temptation was to do this:
>>
>> **************************************
>> sub isDate {
>>
>> $_ = shift;
>> if (m!\d{2}/\d{2}/\d{2}!) { return 1; }
>> else { return 0; }
>>
>>
>> }
>
> Why is this in a subroutine at all? If you are using it like:
[stuff cut out]

Understood, this was just a simplified example. I didn't want to put a
200-line subroutine in an email...



>> **************************************
>>
>> ... 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?



>> Oddly, perl won't let me do "my ($_) = shift;", so I'm stuck having to use
>> another variable.
>
> Perl 5.10 *will* let you do "my $_".

Why is perl on OS X still at 5.8.8? It's free, right? why wouldn't Apple
include the latest one? (I know this isn't an Apple list, just wondering if
anyone knows.)

Thanks, John, et al. -- always tremendously helpful, and much appreciated.

- Bryan



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


Google Custom Search

News | Mail Home | sitemap | FAQ | advertise