5.1 Using Subroutines

     

The most basic subroutine declaration is simply the sub 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 & sigil before the subroutine name in a call will not turn off signature checking. In fact, in most contexts prefixing the subroutine name with & will return a reference to the subroutine instead of calling the subroutine.



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