On Tue, 2 Jul 2002, Michael G Schwern wrote:

> On Fri, Jun 28, 2002 at 01:21:50PM -0700, Erik Steven Harrison wrote:
> > Over on Perlmonks someone was asking about Perl 6's ability to have named
> > argument passing. He also asked about the Jensen Machine and Ruby iterators.
> > Now, just being on this list has taught me so much, but, I'm not quite sure
> > how it works, practically speaking, and whether or not we'll get in in P6.
> > (I understand the abstract o fpass by name, but not how we use it). Could
> > someone explain it to me, and tell me what the Perl 6 stance on the matter
> > is?
> 
> * 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.

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.  If there are no 
arguments, the sub takes one argument and it is aliased to $_. Am I 
right?

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

> 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.  If you wanted to make things 
interesting, you could do it this way too.

        @stuff = grep -> $foo { length $foo >= 42 } @list;

> * Yes, Perl 6 will have the moral equivalent to Ruby iterators.
> 
> 
> Ruby also has it's |$a| mechanism to name the arguments.  Presumably, you'll
> get the same effect with the implied arguments I mentioned earlier.
> 
>     File.foreach '/usr/dict/words' {
>         print $^line;
>     }
> 
> Something like that.

Or

        File.foreach '/usr/dict/words' -> $arg {
                print $arg;
        }


Luke

Reply via email to