On Tue, Nov 19, 2002 at 07:45:25AM +1100, Damian Conway wrote:
: >What <mumble> might be is an interesting, er, topic.
: 
: I would argue it ought to be just $_, which is, after all,
: the One True Topic. And conveniently lexically predeclared in all scopes.
: 
: I would also argue that it ought not be called anything else.
: Surely don't want two specially named variables for the topic?

I actually had an entire ramble on that subject that I deleted from
my previous message before I wrote what I wrote.  But the gist of
it was similar, in that $_ is of indeterminate meaning in a signature
anyway, so why not force it to mean what we want it to mean?  The
long and the short of it was that

    my sub foo ($_ := $arg = $_)

is how you might set $arg to be both the "topic" and the "given".
In the common case, that comes down to

    my sub foo ($_ = $_)

to just propagate the outer $_ inward.  But it still has the problem that
it looks like a declaration of $_ followed by a use of $_ that *isn't* that
declaration.  So I was thinking it'd be better to use something different to
represent the outer topic, like:

    my sub foo ($_ = $^)
    my sub foo ($_ = $+)
    my sub foo ($_ = $|)
    my sub foo ($_ = $=)
    my sub foo ($_ = $:)
    my sub foo ($_ = $?)
    my sub foo ($_ = $$)
    my sub foo ($_ = $@)

or some such.  That's what I meant by <mumble>.  I kinda like $= at the
moment, presuming it's coming available from formats.  Or maybe $?.

Possibly we have something more evil than that, such as the notion that
$? is a double sigil that pulls a name out of the dynamic context's
lexical scope.  Then we could write $?_ for the outer $_, $?foo for the
outer $foo, @+_ for the outer @_, $?! for the outer $!, etc.  That would
make it

    my sub foo ($arg is topic = $?_)

I suppose it could be argued that defaulting the outer topic should also
set the inner topic by default, so you could write

    my sub foo ($arg = $?_) {
        when 1 { ... }
        ...
    }   

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.  But if the signature for a bare block is ($_ = $?_), and
if the = there is really a binding operator, then the inner $_ is just
an alias for the outer one, and it doesn't really matter in that case.

I'm not sure whether we should allow $?_ in a sub body, however.

    for @sortparms {
        &closure := { $?_
                   j  ?? $^a <=> $^b
                      :: $^b <=> $^a
                    };
        sort &closure, @list;
    }

In this case, $?_ would be synonymous with $OUTER::_, since the block is
called in the same lexical context.  But that wouldn't necessarily always
be the case.  We can assume the outer dynamic scope also has a $_ in its
lexical scope, but assuming there's a $?foo out there is more problematic.

But maybe that's one way to explicitly export symbols:

    module Foo;
    &?foo := &my_foo;

That assumes we can tell the the outer scope is being compiled
currently.  It would have the same restrictions as mucking around
with caller.MY, basically.

But don't worry, I still think normal export should be via properties
on the declaration of sub my_foo.

Larry

Reply via email to