[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]

Re: [ID 20000123.003] [BUG our() 5.005_63] Lexical scoping problems.



>               An our declares the listed variables to be valid
>               globals within the enclosing block, file, or eval.
>               That is, it has the same scoping rules as a "my"
>               declaration, but does not create a local variable.

>as basically, "it scopes just like a my() variable except you can
>refer to it as a fully qualified variable"

>Okay, so color me stupid, but why is our()'s lexical scoping
>capabilities useful?  Just for 'strict vars' reasons?

Because you're providing explicit access to a global variable.

    if ($x < 0) {
	our $count;
	$count++;
    } 

    if ($x > 0) {
	print "got $count\n";  # ERROR; $count not seen
    } 

If it this aspect of my() which our() mimics--it's restricting
access to the enclosing scope.   You might also be wrongthinking
by expecting our() to nest.  They don't.  There's only ever one
our() variable.  our() just says "ok to access global in this block".
If another block says the same thing, they're talking about the
same global variable.

my() is completely different.  It can nest.  You can have more than
one my() variable of the same name due to nesting, and these are
different variables, with different addresses, and with potentially
different values.  If another block says the same thing, they're
*not* talking about the same variable, but two separate private
variables of the same name.

my() variables "belong" to private and unique lexical scopes, whereas
our() variables "belong" to packages, the home of global variables and
universally accessible.

    	Perl	=>  Meaning
	----- 	    -------
	my	=>  local
	our	=>  global
	local	=>  save

--tom


[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]