On Fri, Apr 12, 2002 at 05:34:13PM -0700, Glenn Linderman wrote:
> Allison Randal wrote:
> > > In a message dated Fri, 12 Apr 2002, Glenn Linderman writes:
> > > > $_ becomes lexical
> 
> > Sound logic. And it almost did go that way. But subs that access the
> > current $_ directly are far too common, and far to useful.
> 
> One thing I'm missing is how those common useful subs that access the
> current $_ directly will be affected by $_ becoming lexical...
> 
> Or perhaps $_ stays global, but gets automatically "restored" at the end
> of blocks in which it is changed, so that it appears lexical in value,
> even though it is global in the name space ???
 
No, it is truly lexical. Absolutely, positively. 

And you're right that this should present a problem. The lexical scope
of a subroutine is the lexical scope where it was defined, not the
lexical scope where it was evaluated. So, typical subs can only access
global external variables, while closures and lexical subs access
external variables from the scope in which they were defined. 

I suspect I've imperfectly represented the original idea, but I'll
proceed as devil's advocate, for the sake of discussion...

What if $_ were dynamically scoped, but only for subroutines? Dynamic
scoping is not necessarily the same thing as a global $_. It would
merely pretend (only for $_) that the subroutine had been defined in the
scope where it was evaluated. But that could get you into trouble with
recursion. So then you would want some way to explicitly either turn on,
or turn off, dynamic scoping for a specific subroutine. The C<is given>
(or C<is topic>) property would be an obvious way to turn it off (since
it would create a $_ scoped to the subroutine). But, what if you wanted
it off by default? It could be turned on either as a characteristic of
the sub as a whole, or as a characteristic of an individual parameter:

        sub foo is dynamic_topic {
                ...
        }

        sub foo ($bar is topic_preserving, $baz) {
                ...
        }

The property C<is topic_preserving> would be both binding the value of
the $_ in the calling scope to $bar and making $bar the current topic in
the scope of the sub.

The subroutine property would be basically the same, but wouldn't give
you a named variable, and also wouldn't require you to include an extra
parameter (which could be nice).

Allison

Reply via email to