Perl Debate Time ;-)

Arthur Corliss corliss at sinbad.net
Sat Mar 20 15:37:00 CST 1999


On Sat, 20 Mar 1999, Tony wrote:

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

I agree, and I get far too much use out of it for it to be dropped.

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

While I like the local operator, let me play devil's advocate and repeat some
of the arguments I've heard:

	1)  Global variable space should be unpolluted as possible--any 
	    called subroutines should rely more on passed arguments than
		the checking of global variables.  If a global must be played
		with locally, copy the value to a 'my' variable and pass as an
		argument.
	2)  Readability suffers.  Perusing code is more difficult since you
	    now have to attempt to track the scope of variables by the same
		name.

Comments?

	--Arthur Corliss
	  Bolverk's Lair -- http://www.odinicfoundation.org/arthur/
	  "Live Free or Die, the Only Way to Live" -- NH State Motto




More information about the Anchorage-pm mailing list