On Tue, 2005-03-29 at 11:38, Juerd wrote:
> Luke Palmer skribis 2005-03-29  6:14 (-0700):
> >     method iterate () {
> >         for (@.objs) {
> >             .process($_);   # oops
> >         }
> >     }
[...]
> $_ is the topic, and I think that if we have two topics, Perl gets as
> convoluted as Japanese for someone who doesn't understand the language.

I've put a lot of thought into this one since topics were first
broached. It's been nagging at me, and I think I see a solution.

Use "given" as a statement (not a block control operator):

        given :invocant;

This binds $_ to the current invocant.

        given 1;

given maintains a stack (which, itself is lexically scoped, so you don't
get to reach into a caller's givens), so here we request the 1th
(zero-indexed, of course) context.

If we call given's stack @_given, then the above statement is just:

        $_ := @_given[1];

Any implicit binding of $_ other than through the use of "given" inserts
the new value into the stack, so you can do:

        given 1;
        given 0;
        given 1;

Without changing the given stack (important for maintainable code).

This is distinct from:

        given 1 { ... }

In which we bind $_ to the number 1.

This also works with labels:

        OUT: for @a {
                IN: for @b {
                        given OUT; say "Outer scope {$_}";
                        given IN; say "Inner scope {$_}";
                }
        }

by determining the @_given entry that was active in the given scope (ok,
so @_given actually contains more information than just the value
itself).

It would be potentially interesting to introduce a "give" keyword for
this purpose:

        map {map {give 1} 0..1000} @foo;

which simply returns the value of {given 1;$_}

-- 
Aaron Sherman <[EMAIL PROTECTED]>
Senior Systems Engineer and Toolsmith
"It's the sound of a satellite saying, 'get me down!'" -Shriekback


Reply via email to