[comp.lang.perl] Of scoping of local variables and parameters

anneb@zogwarg.etl.army.mil (Anne Brink) (03/13/91)

Apologies if this has been gone over recently, news here just went through
another "sporadic delivery" phase.

Am I missing some concept about perl scoping here?  This behavior seems
strange to me:

When I invoke

{
$answer = "Block of text";
&mysubroutine($answer);
}

and have defined later on:

sub mysubroutine {
	local ($answer, $othervariablename);
	local ($nam) = @_;
	print "Parameter passed = $nam.\n";
	}

I get as output:

Parameter passed = .

But when I switch the local() declarations, like so:
sub mysubroutine {
	local ($nam) = @_;
	local ($answer, $othervariablename);
	print "Parameter passed = $nam.\n";
	}

I get instead:

Parameter passed = Block of text.

It looks a lot like I'm tromping on the scope of variable $answer somehow.
Changing local ($answer) to local ($anythingelse) in mysubroutine unsuprisingly
produces the second result.  I wouldn't have expected the parameter's name to 
have any relevance inside my subroutine, because it's been assigned to @_, or 
so I understand.  (Yes, I know I probably should have the assignment to @_ 
before the other variable declarations.  My editor must have moved them when I 
wasn't looking.(-:)

I'm using 3.0pl44 on SunOS 4.1 on a Sun 3, compiled with gcc 1.37.1

I looked through my camel book and the FAQ, and could not find an appropriate 
enlightening passage.  I'd appreciate any eplanations someone can send my 
way.  E-mail answers would be preferred.

					Thanks in advance,

					-Anne Brink
-- 
###############################################################################
Anne Brink			#	U.S. Army Engineer Topographic Labs
anneb@etl.army.mil		#	Ft. Belvoir, VA (USA)
###############################################################################