@@ -643,9 +643,10 @@ X<|positional argument>
643
643
X < |named argument >
644
644
= head2 Positional vs. named arguments
645
645
646
- An argument can be I < positional > or I < named > . All arguments are positional,
647
- except slurpy hash and arguments marked with a leading colon C < : > .
648
- The latter is called a L < colon-pair|/type/Pair > .
646
+ An argument can be I < positional > or I < named > . By default, arguments are
647
+ positional, except slurpy hash and arguments marked with a leading colon C < : > .
648
+ The latter is called a L < colon-pair|/type/Pair > . Check the following signatures
649
+ and what they denote:
649
650
650
651
$ = :($a); # a positional argument
651
652
$ = :(:$a); # a named argument of name 'a'
@@ -658,15 +659,26 @@ arguments are declared.
658
659
sub pos($x, $y) { "x=$x y=$y" }
659
660
pos(4, 5); # OUTPUT: «x=4 y=5»
660
661
661
- In the case of named arguments and parameters, only the name is used for
662
- mapping arguments to parameters. If a fat arrow is used to construct a
663
- L < Pair|/type/Pair > only those with valid identifiers as keys are recognized
664
- as named arguments.
662
+ In the case of named arguments and parameters, only the name is used for mapping
663
+ arguments to parameters. If a fat arrow is used to construct a
664
+ L < Pair|/type/Pair > only those with valid identifiers as keys are recognized as
665
+ named arguments.
665
666
666
667
= for code :allow<L>
667
668
sub named(:$x, :$y) { "x=$x y=$y" }
668
669
named( y => 5, x => 4); # OUTPUT: «x=4 y=5»
669
670
671
+ You can invoke the routine using a variable with the same name as the named
672
+ argument; in that case C < : > will be used for the invocation so that the name of
673
+ the variable is understood as the key of the argument.
674
+
675
+ sub named-shortcut( :$shortcut ) {
676
+ say "Looks like $shortcut"
677
+ }
678
+ named-shortcut( shortcut => "to here"); # OUTPUT: «Looks like to here»
679
+ my $shortcut = "Þor is mighty";
680
+ named-shortcut( :$shortcut ); # OUTPUT: «Looks like Þor is mighty»
681
+
670
682
It is possible to have a different name for a named argument than the
671
683
variable name:
672
684
0 commit comments