@@ -311,7 +311,7 @@ sub f(&g:($)) {
311
311
f(-> $n { say "Hi {$n}" }); # Explicit argument
312
312
f -> $n { say "Hi {$n}" }; # Explicit argument, no parenthesis
313
313
314
- # Additionally, if 'f' is a method on instance 'obj' you can use C < : >
314
+ # Additionally, if 'f' is a method on instance 'obj' you can use ':'
315
315
# instead of parenthesis
316
316
my $obj; ...;
317
317
$obj.f(-> $n { say "Hi {$n}" }); # Explicit argument
@@ -321,15 +321,28 @@ $obj.f: -> $n { say "Hi {$n}" }; # Explicit argument, no parenthesis
321
321
= head3 C < * > Slurpy params / argument expansion
322
322
323
323
In Ruby you can declare an argument to slurp the remainder of the passed
324
- parameters into an array using a C < * > prefix. It works the same way in Perl 6:
324
+ parameters into an array using a C < * > prefix. It works similarly in Perl 6:
325
325
326
326
= for code :lang<ruby>
327
327
def foo(*args); puts "I got #{args.length} args!"; end # Ruby
328
328
= for code
329
329
sub foo(*@args) { say "I got #{@args.elems} args!" } # Perl 6
330
330
331
+ The Perl 6 version above is slightly different in that when it slurps in the
332
+ arguments into @args, one level of nesting, if any, is automatically removed:
333
+
334
+ = for code
335
+ sub foo(*@args) { dd @args }
336
+ foo([1, [2, 3], 4], 5, [6, 7]); # [1, [2, 3], 4, 5, 6, 7]
337
+
338
+ To preserve the structure of the arguments, you can use C < ** > :
339
+
340
+ = for code
341
+ sub foo(**@args) { dd @args }
342
+ foo([1, [2, 3], 4], 5, [6, 7]); # [[1, [2, 3], 4], 5, [6, 7]]
343
+
331
344
You might want to expand an array into a set of arguments. In Perl 6 this is
332
- also done using C < * > :
345
+ done using a L < Slip|/type/Slip > C < | > :
333
346
334
347
= for code :lang<ruby>
335
348
args = %w(a b c) # Ruby
@@ -338,7 +351,7 @@ foo(*args)
338
351
= for code
339
352
sub foo($q, $r, $s) { ... };
340
353
my @args = <a b c>; # Perl 6
341
- foo(@* args);
354
+ foo(|@ args);
342
355
343
356
Perl 6 has many more advanced ways of passing parameters and receiving
344
357
arguments, see L < Signatures|/language/functions#Signatures > and
@@ -355,7 +368,7 @@ $!foo # Private instance variable
355
368
$.foo # Instance variable accessor
356
369
$*foo # Dynamically scoped variable
357
370
$^foo # A positional (placeholder) parameter to a block
358
- $:foo # A named parameter
371
+ $:foo # A named (placeholder) parameter to a block
359
372
$=foo # POD (documentation) variables
360
373
$?FILE # Current source filename. The ? twigil indicates a compile-time value
361
374
$~foo # Sublanguage seen by parser, uncommon
0 commit comments