Reputation: 1539
When specifying formal parameters for a subroutine in perl, I am aware of the following notation. I'm not sure entirely what they mean, but form context clues and seeing other's explain their code, I have deduced this much:
sub method1($$){...} <-- Means it takes in two scalar parameters
sub method2(@){...} <-- Means it takes in a bunch of parameters as a hash
sub method3($@){...} <-- Menas it takes in a scalar parameter, then a bunch of other parameters as a hash.
However, I have also found this notation and am unaware of what it means:
sub method4(@;$)
What, functionally, does the formal parameter declaration of @;$
do that @$
does not?
Upvotes: 1
Views: 86
Reputation: 9231
This feature is called prototypes, and it is not a formal parameter specification, but instead a ruleset for the parser of how to parse arguments passed to your subroutine, which also happens to do some rudimentary arity checking and coercion (sometimes in ways that will be surprising to the user). It is explained in that feature's documentation:
A semicolon (;) separates mandatory arguments from optional arguments. It is redundant before @ or %, which gobble up everything else.
It is often simpler to not use prototypes at all than risk the confusion that they can cause. Some examples: a $
parameter will be forced into scalar context even if it is an array, and the prototype is ignored entirely when the subroutine is called as an object or class method (because it is not yet determined what subroutine will be called at the time the call is parsed).
For formal parameter specifications, use the signatures feature, or Function::Parameters (which currently has the benefit of being feature-complete - more features for signatures are coming soon).
Upvotes: 4