On Tue, Jul 02, 2002 at 07:32:00PM -0600, Luke Palmer wrote:
> On Tue, 2 Jul 2002, Michael G Schwern wrote:
> 
> > * Yes, Perl 6 will have named arguments to subroutines.
> > 
> > What I can remember from the Perl 6 BoF is it will look something like this:
> > 
> >     sub foo ($this, $that) {
> >             print $this if $that;
> >     }
> > which is like:
> > 
> >     sub foo {
> >         my($this, $that) = @_;
> >     print $this if $that;
> >     }
> > 
> > somebody else on this list can handle explaining how that all works better
> > than I can.  There's stuff about pointy subroutines, ->, method topics,
> > etc... *hand wave*
> > 
> 
> Yep (far as I know. The only way I can secure my knowledge is to be 
> arrogant and then be confirmed or negated). After this, you can call foo 
> like so:
> 
>       foo("Bar", 1);
> 
> Or like this:
> 
>       foo(that => 1, this => "Bar");
> 
> Or like this:
> 
>       foo("Bar", that => 1);
> 
> They all do the same thing.

Yes.

> As far as pointy subs, -> is just a synonym for sub, with some extra sugar 
> sprinkled on.  You don't need to put parenthesis around the arglist, and I 
> think $_ is always aliased to the first argument.  

Yes, yes and yes.

> If there are no arguments, the sub takes one argument and it is
> aliased to $_. Am I right?

If you think you want a pointy sub with no parameters, like:

        for @stuff -> { print; }

        @stuff = grep -> { /blarf/ } @list;

        %commands = (
                add  = -> { $^a + $^b },
                incr = -> { $_ + 1 },
        )

what you really want is a bare closure:

        for @stuff { print; }

        @stuff = grep { /blarf/ } @list;

        %commands = (
                add  = { $^a + $^b },
                incr = { $_ + 1 },
        );

which will alias $_ to the first argument. The "pointy sub with no
parameters" may even be a syntax error.

(Methods do act like you suggested, because even when they have no
parameter list or explicit arguments, they still have 1 argument -- the
invocant, a.k.a. $self -- and it is aliased to $_.)

> Of course you can't make named subs with ->, just anonymous ones.

Yes.

> > I *think* you will also be able to do this, at least I can't see why you
> > wouldn't be able to:
> > 
> >     @stuff = grep { length $^foo >= 42 } @list;
> > 
> > which is nice for nested greps and maps.  You don't have to fight over who
> > has $_.
> 
> Yep, grep takes a closure argument now.  

And if Larry goes with what he suggested last Thursday, that may be the
only type of argument it takes in that position, no more bare
expressions. Two curly-braces seem a small price to pay for the gains in
consistency.

> If you wanted to make things interesting, you could do it this way
> too.
> 
>       @stuff = grep -> $foo { length $foo >= 42 } @list;
 
I'd even say that's the preferred way of doing it, though it's more a
matter of style.

<tips hat to Luke> Nicely explained.

Allison

Reply via email to