Perl6: variable number of arguments to function/subroutine

  • A+
Category:Languages

I want to be able to run a function with a variable number of parameters in Perl6, but after reading through https://docs.perl6.org/language/functions#Arguments I don't see how it can be done. I see numerous links for other languages, and a warning that "Questions with similar titles have frequently been downvoted" but I don't see this anywhere in the documentation or StackOverflow.

I want to do something like this:

some-test($v1, $v2) 

or

some-test($v3) 

but not have a separate function using multi for each

How can I construct a Perl6 subroutine which will accept a variable number of strings?

 


It sounds to me like you're talking about this:

sub slurpy-aka-variadic (**@arguments) { # **double** asterisk: **@...   .say for +@arguments, @arguments }  slurpy-aka-variadic 1;                 # 1␤[1]                1 arg slurpy-aka-variadic 1, 2;              # 2␤[1 2]              2 args slurpy-aka-variadic(1, 2);             # 2␤[1 2]              2 args slurpy-aka-variadic (1, 2);            # 1␤[(1 2)]            1 arg slurpy-aka-variadic (1, 2), (3,4);     # 2␤[(1 2), (3 4)]     2 args  sub slurpy-aka-variadic-flattening (*@arguments) { # *single* asterisk   .say for +@arguments, @arguments }  slurpy-aka-variadic-flattening 1;              # 1␤[1]        1 arg slurpy-aka-variadic-flattening 1, 2;           # 2␤[1 2]      2 args slurpy-aka-variadic-flattening (1, 2);         # 2␤[1 2]      2 args slurpy-aka-variadic-flattening (1, 2), (3,4) ; # 4␤[1 2 3 4]  4 args  sub slurpy-aka-variadic-strings (**@arguments where .all ~~ Str) {   .say for +@arguments, @arguments }  slurpy-aka-variadic-strings '1';        # 1␤[1] slurpy-aka-variadic-strings '1', '2';   # 2␤[1 2] slurpy-aka-variadic-strings ('1', '2'); # Constraint type check failed 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: