# Copyright (C) 2003-2007, The Perl Foundation. =pod =head0 Subroutines Z X Subroutines are reusable units of code, and one of the fundamental building blocks of modern programming languages. They can be called from just about anywhere, and return control to the point of the call when they finish executing. They can be passed zero or more argumentsN and return zero or more results. Some programmers may be more familar with languages which only allow a single return value from a subroutine, or languages for which subroutines have exactly zero return values and "functions" may have one. Perl 6 generalized the concept to allow subroutines to return as many, or as few, return values as needed. We think it just makes more sense to let the programmer do what they want to do in this regard. Subroutines can be named or anonymous. They can be lexically scoped, package scoped, or globally scoped. Subroutines can be "Multi" subs, which allow multiple subroutines to have the same name as long as they have different parameter lists. Blocks of reusable code can also be called the "methods" of a particular class of object. Methods have a few significant differences from subroutines, and a few significant differences from those found in Perl 5. For instance, in Perl 6 they're distinguished by a separate keyword, C. Because of these differences, they'll be discussed in AChapter 6. =head1 Using Subroutines Z X The most basic subroutine declaration is simply the C keyword, followed by the name of the sub, followed by the block that defines the sub: sub alert { print "We have normality."; } The simplest subroutine call is just the subroutine name followed by a comma-separated list of variables or values: $result = sum($a, $b, 42, 57); Arguments are also sometimes passed as anonymous pairs: $result = sum(first => 12, second => 21); Parentheses are optional on calls to subroutines that have been predeclared, but required for all other calls. Including the C<&> sigil before the subroutine name in a call will not turn off signature checking as it did in Perl 5. In fact, in most contexts prefixing the subroutine name with C<&> will return a reference to the subroutine instead of actually calling the subroutine. =head1 Parameters Z X X X X One of the most significant additions to subroutines in Perl 6 is formal parameters. The parameter list, often called the I of the subroutine, is part of the subroutine declaration: sub standardize ($text, $method) { my $clean; given $method { when 'length' { $clean = wrap($text, 72); } when 'lower' { $clean = lowercase($text); } ... } return $clean; } The subroutine C has two scalar parameters, C<$text> and C<$method>, so it is called with two arguments, each a scalar value. The parameters are lexical variables within the body of the sub. They never need to be explicitly declared as C, even under C because they're declared by the subroutine declaration. X<@ (at sign);@_ array> In a sub with no parameter list, all arguments are passed in the C<@_> array: sub sum { my $sum; for @_ -> $number { $sum += $number; } return $sum; } Subroutines with defined parameter lists don't get an C<@_> array.N. See A"Variadic parameters" later in this chapter.> If you want a subroutine that takes no arguments (and complains when arguments are passed), define it with an empty parameter list C<()>. Subroutine calls normally provide a non-flattening list context, which means that any array or hash passed into a subroutine is treated as a single argument. An array parameter in the signature expects to be passed an actual array or arrayref argument, and a hash parameter expects a hash or hashref argument: sub whole (@names, %flags) { ... } whole(@array, %hash); =head2 Optional Parameters Z X X Every subroutine call checks its signature to make sure the arguments it gets match the declared parameter list. A mismatch in the number or kind of arguments is an error. But since requiring every declared parameter to be passed in on every call isn't nearly flexible enough for the average programmer, Perl 6 also allows optional parameters. Optional parameters can be included or ignored without causing any errors. Each optional parameter is marked with a C after the parameter name: sub someopt ($required1, $required2, $optional1?, $optional2?) { ... } So, C will accept anywhere from 2 to 4 arguments. You can have any number of required and optional parameters, but all the required parameters must come before the first optional parameter. This is largely a common sense restriction. If you want to leave some elements off a list of arguments, it has to be the ones at the end, because positional arguments are bound to the parameters in strict linear order. All these calls to C are fine: someopt('req1', 'req2', 'opt1', 'opt2'); someopt('req1', 'req2', 'opt1'); someopt('req1', 'req2'); Notice that it will still cause an error to pass too many or too few parameters in a list with optional parameters: someopt('req1') # WRONG! someopt('req1', 'req2', 'opt1', 'opt2', 'extra') # WRONG! =head2 Named Parameters Z X X Any argument can be passed either by position with an ordered list of arguments, or by name with an unordered list of pairs (see A"Named Argument Passing" later in this chapter for more details). Sometimes you'll want to specify that certain parameters will be passed only by name, never by position. Named parameters are marked with a C<:> before the parameter name: sub namedparams ($first, :$second, :$third) { ... } namedparams(1, second => 2, third => 3); Named parameters are always optional. They must come after all positional parameters--that is, after the unmarked required parameters and the optional parameters marked with a C. Again, this is largely a matter of common sense. Though named parameters are completely ignored when binding a list of positional arguments, the parser and the person maintaining your code will both be profoundly grateful they don't have to sort through a mixed bag of positional and named parameters to find the positional parameter list. If it makes more sense to do it, you can also use the alternate key syntax for passing parameters: namedparams(1, :second(2), :third(3)) # Right Also, named parameters aren't positional, you can pass them in any orderN. So, we can pass the second third and the third second: namedparams(1, third => 3, second => 2) The point of having a name for your parameter is that you don't have to worry about the position of it. =head2 Variadic Parameters Z X X Another element of flexibility Perl developers will expect is the ability to pull a list of arguments into an array or hash parameter. These are known as I parameters because they can take a variable number of arguments. In Perl 6, an array parameter with a C<*> before the parameter name will slurp up all the I arguments that haven't already been bound to another positional parameterN"Referencing (or Not)" in Chapter 4.>. So, the following call to C binds C<$arthur> to C<@names[0]>, and C<$ford> to C<@names[1]>: sub transport ($planet, *@names) { ... } transport('Magrathea', $arthur, $ford); If the variadic array parameter is the only positional parameter in the signature, it will take all the positional arguments. sub simple (*@_) {...} # is the same as sub simple {...} A hash parameter with a C<*> before the name will slurp up all the I arguments that haven't already been bound to another parameter. So, the following call to C binds the value of the pair argument with the key C<'planet'> to the parameter C<:$planet>, but all the other pairs become part of the C<%flags> hash (more on this in A"Named Argument Passing" later in this chapter). sub transport (:$planet, *%flags) {...} transport(:name('Arthur'), :luggage('lost'), :planet('Magrathea'), :towel('required')); When they're combined with other kinds of parameters, variadic positional parameters must come after all positional parameters in the signature. They can either precede or follow the named parameters. Variadic named parameters only slurp up the named parameters that aren't bound already, so they can appear anywhere in the signatureN. =head2 Typed Parameters Z X X Signature checking is sensitive not only to the number of arguments and the variable type (defined by the C<$>, C<@>, C<%>, or C<&> symbol), but also to the value typeN"Types" in Chapter 4 for more details on value and variable types.>. The parameter type is defined before the parameter name and before any symbols for optional, named, or variadic parameters: sub typedparams (Int $first, Str $second?) {...} The parameter type declares the type of argument that can be bound to it. The parameter and argument types have to be compatible, but not identical. Type checking happens at compile time whenever possible, because it's faster and can be optimized. Otherwise, type checking happens at run time. So, if all the arguments passed to the subroutine are explicitly typed, the types will be checked at compile time. If the arguments aren't explicitly typed, the run-time checks will make sure the scalars contain an integer value and a string value. =head2 Properties on Parameters Z X X By default, parameters are aliases to the original arguments (pass-by-reference), but they're marked as constant so they cannot be modified within the body of the subroutine. The C property marks a parameter as modifiable, so changes to the parameter within the body of the sub modify the original variable passed in: sub modifyparams ($first is rw, $second is rw) {...} Be careful about using the C property unless it's necessary, inadvertent changes in a subroutine can change data values in the caller's scope that might not be expecting to be changed. At the very least, it's polite to document it when your subroutine is monkeying around in somebody else's scope. The C property marks a parameter as pass-by-value, so the parameter is a lexically scoped copy of the original value passed in: sub passbyvalue ($first is copy, $second is copy) {...} This means that the parameter is not an alias for the value in the caller's scope, but it's not read-only either. It's a new variable, a copy of the original and free to be used however your subroutine sees fit. =head2 Default Values for Parameters Z X Sometimes it is useful to be able to define a default value for an optional or named parameter. The C<=> operator marks the default valueN symbol in a different context.>. The parameter takes the default value only if the call doesn't pass an argument for that parameter. sub default_vals ($required, $optional? = 5) {...} Default values are only used with optional parameters. This should make sense because required positional values are always required, and so they always have a value passed. =head2 Placeholder Variables Z X X X<^ (caret);in placeholder variable names> X<$ (dollar sign);$^ in placeholder variable names> Placeholder variables are a simple alternative to formal parameter lists. They have many of the advantages of ordinary parameters, without the inconvenience of declaring a signature. You just use variables with a caret after the sigil--C<$^name>, C<@^name>, C<%^name>, or C<&^name>--within the subroutine's block, and the arguments passed into the subroutine are bound to them. @sorted = sort { $^a <=> $^b } @array; The order of the parameters is determined by the Unicode sorting order of the placeholders' names, so the example below acts as if it has a formal parameter list of C<($^milk, $^sugar, $^tealeaves)>: $make_tea = { my $tea = boil $^tealeaves; combine $tea, $^sugar, $^milk; return $tea; } Placeholders are handy in short subroutines and bare blocks, but soon become unwieldy in anything more complicated. =head2 Return Values Z In addition to a signature for the incoming parameters to a subroutine, you can also declare a partial signature, or I, for the values returned from a subroutine. Return siglets declare the type of each return value, but they don't bind a named variable to the returned value and can't define a default value for the return. In the declaration, the return signature either goes before the C keyword or after the parameter list attached with the C keyword. sub get_value (Int $incoming) returns Int {...} # same as Int sub get_value (Int $incoming) {...} # same as sub get_value (Int $incoming --> Int) {...} Either syntax has exactly the same effect, but using the C keyword is usually clearer when the sub has multiple return values: sub get_values (Str $incoming) returns (Int, Str) {...} =head1 Arguments Z X X 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 =head2 Named Argument Passing Z 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 I