[oak perl] still clueless about "my"

Sandy Santra santranyc at yahoo.com
Fri Feb 4 04:25:54 PST 2005


Thanks for looking at my code and offering those great suggestion
 and insights, Belden.  All great stuff--some of it I'm
already using, some of it I'm studying.   

But you know, I'm still clueless about using the "my" operator
for purposes of scoping.  I've studied the definition and
the examples and it's all very confusing. 

*Your* example makes total sense:

>
>   foreach my $file ( glob '*' ) {
>       my $newfile = $file ;
>   }

because you put the "my" definition within the If statement.
 But so often in code I see quoted, the "my" statement is
put at the very beginning of the quoted code, before the
"if" (or sub or while or whatever) statement even starts--so
how can that "my" delcaration scope to something?  Here's
an example: 

my $princess = "I dream of Jill.";
if ($princess =~ /ill\b/) {
	print "Jill might be ill.\n";
}

Now what if this If statement was followed by 6 other If
statements...  Does some sort of invisible tether link the
"my" declaration in the beginning to *only* the very first
If statement? 

Here's another example:

my $bedrock = 42;
$bedrock++;

How and what is the "my" declaration scoping in this example?
 (This one makes me want to tear my hair out.)  If this
piece of code was dropped into a huge piece of code, I don't
understand how it would scope the definition of $bedrock
to anything. 

This "my" scoping confusion I have is frustrating because
I know that scoping is very important.  "my" is used in
almost every piece of code I read in Perl forums and instruction
manuals.  But half the time the "my" declaration is in the
middle of nowhere, and I can't fathom why it's there or
what it's affecting.  Boy, I need help with this. 

Alright, I'm going to eat some worms now.

Back to the camel book.

--Sandy





More information about the Oakland mailing list