September 2017
Beginner
402 pages
9h 52m
English
Even if the signature of the sub is missing, the sub can still take and use parameters. Perl 6 defines the so-called placeholders, which are the variables with the ^ twigil inside a sub . We can see this in the following code:
sub greet {
say "Hello, $^name!";
}
greet('Mark'); # Hello, Mark!
In this code, the $^name variable takes the value of the string passed at the function call. The value becomes a read-only parameter of the sub.
If there is more than one parameter, their order corresponds to the alphabetical order of the placeholders:
sub subtract {
$^b - $^a
}
say subtract(10, 8); # -2
The values 10 and 8 reside in the $^a and $^b variables here.
When using placeholders, a function cannot have a signature that ...
Read now
Unlock full access