in reply to YAlQ: Yet Another local() Question.

As far as I can tell, your writeup captures everything about local except the fact that, as an efficiency hack, Perl doesn't use assignment to implement it, but aliasing.

In other words, local $_ is more like:

{ my $saved = \$_; my $new; *_ = \$new; # ... *_ = $saved }
Most of the time, this has the same effect as assignment, but is faster. That's why Perl 5 uses it.

Unfortunately, this improvement in Perl 5 actually broke some code that expected localization to be visible via all names of aliased variables (e.g. exported variables). In short, you can't usefully localize an exported variable in Perl 5. This is a Bad Thing, and I hope that Larry fixes it somehow.

On the other hand, I hope that Larry finally fixes the name of the operator by renaming it to ``save''.

    -- Chip Salzenberg, Free-Floating Agent of Chaos

Replies are listed 'Best First'.
RE: Re: YAlQ: Yet Another local() Question.
by BlaisePascal (Monk) on Aug 04, 2000 at 00:30 UTC
    Ah, that would also explain why there is the IMHO arbitrary restriction that you can only localize global variables, and not lexical ones. Why something like:
    { my $fh; sub init_print_log { $fh = shift;} sub print_log { print $fh scalar localtime,@_;} sub print_log_altfile { local $fh = shift; print_log(@_);} }
    is illegal, even though the -concept-, if not that particular example, is useful.

    UPDATE: Yes, save() would be a better name... better than some of the others that have been suggested. Someone also suggested renaming my() to local(). I hope that one doesn't fly.

      Actually, I have a patch that makes local() work on lexical variables. Larry didn't want to use it because he thought people would be too confused. :-(

      And I wouldn't worry about my() being renamed to local(). Larry chose the word "my" at least in part because it's short and easy to read and write.

          -- Chip Salzenberg, Free-Floating Agent of Chaos