On Tue, Nov 19, 2002 at 03:09:40PM -0600, Allison Randal wrote:
: Larry wrote:
: > I'm trying to remember why it was that we didn't always make the first
: > argument of any sub the topic by default. I think it had to do with
: > the assumption that a bare block should not work with a copy of $_ from
: > the outside.
:
: I dug through the archives. We were considering allowing dynamic scoping
: of $_ across subroutine calls (for backward compatibility with p5), so
: we delayed any final call on whether subs topicalize their first
: argument. We've pretty much resolved the first: such an abuse of lexical
: scope should be explicitly marked.
:
: For consistency, I think the first argument of subs should be the topic by
: default.
This might work now, presuming
sub foo (;$_ = $=)
(or whatever) is really a binding, and not an assignment. (That's another
reason why //= is *wrong*--it implies assignment.)
I guess it's almost an epistemological issue. Suppose you see the
following general code:
$_ = 1;
mumble {
$_ = 2;
}
print $_;
How can you know whether it will print 1 or 2? It will depend on
both the signature of C<mumble>, and the signature of the bare closure
passed to it. Things that are trying to look like C<grep> or C<map>
obviously want to have a private $_. Things that are trying to look
like C<if> or C<while> will want to share $_ with the outside scope.
I suspect the bare block has to have a signature like (;$_ = $=) in the
absence of $^a etc. That means that a bare block always treats $_ as
pseudo-dynamic (by aliasing two lexicals). That means
$sub = {
$_ = 2;
};
$_ = 1;
$sub();
print $_;
would print 2.
But if the bare block always binds $_ inward, it's the wrapper that
actually decides the issue. For something if-like:
sub mumble (█ $_ = $=) {
block();
}
For something grep-like:
sub mumble (&block) {
block()
}
Oddly, if we make the first argument the topic by default, the second
block actually gets &block as its topic. That's...strange. People will
expect it (rightly or wrongly) to be initialized with the outer value,
even if it's just a copy. Hmm...need to think about this s'more...
Larry