Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Same-named arguments

4 views
Skip to first unread message

Michael Snoyman

unread,
Aug 23, 2006, 3:10:23 PM8/23/06
to perl6...@perl.org
Hi,

I'm just starting with Perl 6. I was reading through "Perl 6 and Parrot
Essentials" (finally arrived yesterday from Amazon; very happy) and I was
wondering what would happen if you had a parameter list that included
variables of a different type but the same name (ie, $foo, @foo). I wrote a
little test script and ran it through pugs. Here's what I got:

Script:

use v6;

sub mysub($foo, @foo, %foo) {
say "Starting mysub";
say "Printing scalar";
say $foo;
say "Printing array";
say @foo;
say "Printing hash";
say %foo;
say "Leaving mysub\n";
}

my $foo = 'foo';
my @foo = qw|foo bar|;
my %foo = ( foo => 'bar', foo2 => 'bar2' );

mysub($foo, @foo, %foo);
mysub(:foo($foo), :foo(@foo), :foo(%foo));


Output:

Starting mysub
Printing scalar
foo
Printing array
foobar
Printing hash
foo barfoo2 bar2
Leaving mysub

Starting mysub
Printing scalar
foo
Printing array

Printing hash

Leaving mysub

Just wondering if the language is meant to work that way, or if it's a pugs
"feature."

Thanks,
Michael

Juerd

unread,
Aug 23, 2006, 2:53:38 PM8/23/06
to perl6...@perl.org
Michael Snoyman skribis 2006-08-23 12:10 (-0700):
> sub mysub($foo, @foo, %foo) {

I hope this is a compile time failure. If not, I'd expect a warning, at
least.


Juerd
--
http://convolution.nl/maak_juerd_blij.html
http://convolution.nl/make_juerd_happy.html
http://convolution.nl/gajigu_juerd_n.html

cma...@gmail.com

unread,
Aug 23, 2006, 3:54:37 PM8/23/06
to perl6...@perl.org
Juerd (>), Michael Snoyman (>>):

> > sub mysub($foo, @foo, %foo) {
>
> I hope this is a compile time failure. If not, I'd expect a warning, at
> least.

Why? It looks reasonable IMHO.

// Carl

Juerd

unread,
Aug 23, 2006, 4:23:37 PM8/23/06
to perl6...@perl.org
Carl Mäsak skribis 2006-08-23 21:54 (+0200):

Because arguments are passed without sigil, and here you'd be forcing
positional arguments. I think that's bad.

sub do_something ($foo) { ... }

do_something [ 1, 2 ];
do_something foo => [ 1, 2 ];
do_something :foo([ 1, 2 ]);

sub do_something (@foo) { ... }

do_something [ 3, 4 ];
do_something foo => [ 3, 4 ];
do_something :foo([ 3, 4 ]);

sub do_something ($foo, @foo) { ... }
do_something [ 1, 2 ], [ 3, 4 ];
# But how do you pass named arguments now?

0 new messages