Re: pass hash to sub expecting named params?

13 views
Skip to first unread message

Luke Palmer

unread,
Apr 25, 2005, 4:29:48 AM4/25/05
to ca...@fireartist.com, perl6-language
Carl Franks writes:
> Will it be valid to pass a hash to a subroutine expecting named
> params, if the hash keys match the names?
>
> sub do_this (+$foo, +$bar) {
> # whatever
> }
>
> %arg = (
> :foo,
> :bar,
> );
>
> do_this(*%arg);

Yep, and that's exactly how you do it, too. I believe that the * is
unnecessary (but still acceptable) if you're already in the named zone:

do_this(foo => 1, %arg); # ok

Luke

Carl Franks

unread,
Apr 25, 2005, 4:14:31 AM4/25/05
to perl6-language
Will it be valid to pass a hash to a subroutine expecting named
params, if the hash keys match the names?

sub do_this (+$foo, +$bar) {
# whatever
}

%arg = (
:foo,
:bar,
);

do_this(*%arg);

I use this technique a lot, and it would be unfortunate to miss out on
the advantages of subroutine signatures and have to 'go back' to the
perl5-ish
sub do_this (*%args) { }

Carl Franks

Carl Franks

unread,
Apr 25, 2005, 4:50:49 AM4/25/05
to perl6-language
That puts my mind at ease!
Many thanks,

Carl

Reply all
Reply to author
Forward
0 new messages