Perl Debate Time ;-)

Tony tonyt at ptialaska.net
Sat Mar 20 15:25:50 CST 1999


On Sat, 20 Mar 1999, Arthur Corliss wrote:

> Greetings:
> 
> One of the semi-contentious debates I see on the newsgroups is on the use of
> local vs my.  There's even been some rumbling about having local deprecated,
> though I doubt it'll ever be considered by the higher echelons.
> 
> I'm bringing it up here just to see what our local programmers are doing,
> which they prefer, and what instances they'd use one over the other.
> 
> Personally, I only use local (currently) for private subroutines within a
> subroutine.  I hate polluting namespace.  :-)  My, of course, is used for
> everything else, especially since I religiously use strict mode.
> 
> So, what preferences do you guys have?

I think those who want to get rid of "local" are wrong.  They are two
compoletely different operators, used for two different purposes.

Variables declared with "my" are completely hidden from the outside world,
/even subroutines called within the same block/, and /even if the
subroutine is calling itself recursively./  According to "perldoc
perlsub,"

       NOTE: In general, you should be using "my" instead of
       "local", because it's faster and safer.  Exceptions to
       this include the global punctuation variables, filehandles
       and formats, and direct manipulation of the Perl symbol
       table itself.  Format variables often use "local" though,
       as do other variables whose current value must be visible
       to called subroutines.

A local declaration is used when the variable must be visible beyond the
scope of the enclosing loop, including called subroutines.

Hope this clears up the confusion.....

							- Tony




More information about the Anchorage-pm mailing list