[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl][off topic] local & my (was Re:Any way to Tee...)



At 22.00 1998.02.17, Alex Satrapa wrote:
>At no point is $my_local_var accessible by the main program (which it would
>be if local() == "global scope").

Yes, it is accessible.  But the dynamic scope of the variable had ended by
the time you tested it.

#!perl -w
package A;
sub foo {print $main::local}
package main;
$local = 1;
A::foo();
{
  local $local = 2;
  A::foo();
}
A::foo();

Returns:

  121

If local($local) were not global, then A::foo could not have seen the value
of $main::local as 2.  Perhaps you are using the word "global" differently;
I define it, as do the writers of the Perl books, as a variable that is
visible from anywhere.  This is certainly the case with local()ized
variables.  The question with local()ized variables is not WHERE they are
accessible, but WHEN.  Once its dynamic scope ends, its value reverts to
what it was before.


>Another way of thinking of local() is that it creates a "overrides" a
>previous declaration of a "global" variable.

The terms I like (Tom Christiansen said it on IRC, and likely elsewhere) is
"save and restore".  When you local() a variable, it is still the same
variable as it was before (as evidenced with A::foo()) but it has a new
temporary value, that reverts back to the original value at the end of the
scope.

--
Chris Nandor          mailto:pudge@pobox.com         http://pudge.net/
%PGPKey=('B76E72AD',[1024,'0824 090B CE73 CA10  1FF7 7F13 8180 B6B6'])
#==               New Book:  MacPerl: Power and Ease               ==#
#==    Publishing Date: Early 1998. http://www.ptf.com/macperl/    ==#



***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch