5.3 Arguments

     

The standard way of passing arguments is by position. The first argument passed in goes to the first parameter, the second to the second, and so on:

 sub matchparams ($first, $second) { . . . } matchparams($one, $two);  # $one is bound to $first                           # $two is bound to $second 

5.3.1 Named Argument Passing

You can also pass arguments in by name, using a list of anonymous pairs. The key of each pair gives the parameter's name and the value of the pair gives the value to be bound to the parameter. When passed by name , the arguments can come in any order. Optional parameters can be left out, even if they come in the middle of the parameter list. This is particularly useful for subroutines with a large number of optional parameters:

 sub namedparams ($first, ?$second, ?$third is rw) { . . . } namedparams(third => 'Trillian', first => $name); 

Sometimes the option syntax for pairs is clearer than the pair constructor syntax:

 namedparams :third('Trillian'), :first($name); 

5.3.2 Flattening Arguments

To get the Perl 5-style behavior where the elements of an array (or the pairs of a hash) flatten out into the parameter list, use the flattening operator in the call to the subroutine. Here, $first binds to @array[0] and $second binds to @array[1] :

 sub flat ($first, $second) { . . . } flat(*@array); 

A flattened hash argument acts as a list of pairs, which are bound to the parameters just like ordinary named arguments. So, $first is bound to %hash{'first'} , and $second is bound to %hash{'second'} :

 sub flat_hash ($first, $second) { . . . } %hash = (first => 1, second => 2); flat_hash(*%hash); 

Flattened hash arguments are useful for building up hashes of named arguments to pass in all at once.

5.3.3 Argument Order Constraints

Arguments to subroutine calls have a standard general order. Positional arguments, if there are any, always go first. Named arguments go after any positional arguments. Variadic arguments always go at the end of the list.

 order($positional, named => 1, 'va', 'ri', 'ad', 'ic'); 

Positional arguments are first so the parser and the person maintaining the code have an easier time associating them with positional parameters. Variadic arguments are at the end because they're open -ended lists.

If a subroutine has only required and variadic parameters, you can always call it with a simple list of positional arguments. In this example, ' a ' is bound to $req and the rest of the arguments go to the slurpy array:

 sub straight_line ($req, *@slurpy) { . . . } straight_line('a', 'b', 'c', 'd', 'e'); 

If a subroutine has some optional parameters and a variadic array you can call it with a simple list of positional arguments, but only if you have arguments for all the optional parameters. In this example, ' a ' is bound to $req , ' b ' is bound to $opt , and the rest of the arguments go to the slurpy array:

 sub mixed ($req, ?$opt, *@slurpy) { . . . } mixed('a', 'b', 'c', 'd', 'e'); 

If you want to skip some of the optional parameters, you have two choices. When the argument list has at least one named argument, the parser knows to start the variadic list right after the named arguments end. This example binds ' a ' to $req , binds ' opt ' to $opt , skips $another , and puts the rest of the arguments in the variadic array:

 sub mixed ($req, ?$opt, ?$another, *@slurpy) { . . . } mixed('a', 'opt' => 1, 'b', 'c', 'd', 'e'); 

If you want to skip all the optional parameters you need to use the <= = operator in place of the comma to mark where the variadic list starts. This example binds ' a ' to $req , skips $opt and $another , and puts all the rest of the arguments in the variadic array:

 mixed('a' <=  =  'b', 'c', 'd', 'e'); 

You have to watch out for optional and variadic parameters when you modify subroutines already in use. Adding an extra optional parameter to a signature with a variadic array will break any calls that passed all positional arguments. You could suggest that all users call your subroutines with <= = in case you decide to change them later, or you could just add the new parameters as named parameters instead of optional parameters. Named parameters ignore positional arguments, so this version of the subroutine puts ' b ' through ' e ' in the variadic array with or without any named arguments in the call:

 sub mixed ($req, +$opt, +$another, *@slurpy) { . . . } mixed('a', 'opt' => 1, 'b', 'c', 'd', 'e'); mixed('a', 'b', 'c', 'd', 'e'); 

As usual, there's more than one way to do it.



Perl 6 and Parrot Essentials
Perl 6 and Parrot Essentials, Second Edition
ISBN: 059600737X
EAN: 2147483647
Year: 2003
Pages: 116

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net