Skip to content

Commit 7f16b18

Browse files
committed
[Signature] explain passing of positional and named arguments; parameter aliasing and renaming
1 parent b63269b commit 7f16b18

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

lib/Signature.pod

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,29 @@ except those marked with a leading colon C<:>, and slurpy hash parameters.
8686
:(*@a) # a slurpy positional parameter
8787
:(*%h) # a slurpy named parameter
8888
89+
On the caller side, positional arguments are passed in the same order
90+
as the parameters were declared.
91+
92+
sub pos($x, $y) { "x=$x y=$y" }
93+
pos(4, 5); # x=4 y=5
94+
95+
In the case of named arguments and parameters, only the name is used for
96+
mapping arguments to parameters
97+
98+
sub named(:$x, :$y) { "x=$x y=$y" }
99+
named( y => 5, x => 4); # x=4 y=5
100+
101+
It is possible to have a different name for a named parameter than the
102+
variable name:
103+
104+
sub named(:official($private) { } # parameter name is 'official'
105+
106+
Aliases are also possible that way:
107+
108+
sub paint( :color(:colour($c)) ) # 'color' and 'colour' are both OK
109+
sub paint( :color(:$colour) ) # same API for the caller
110+
111+
89112
=head2 Optional and Mandatory Parameters
90113
91114
Named parameters are optional by default, and can be made mandatory with a

0 commit comments

Comments
 (0)