Subroutines need to be declared, i.e., specified how they should be called, and defined, i.e., specified what they should do when called.
(
proto )
] [ attributes ]Declares name as a subroutine, optionally specifying the prototype and attributes. Declaring a subroutine is optional, but allows the subroutine to be called just like Perl’s built-in operators.
(
proto )
] [ attributes ] blockDefines subroutine name, with optional prototype and attributes. If the subroutine has been declared with a prototype or attributes, the definition should have the same prototype and attributes. When name is omitted, the subroutine is anonymous and the definition returns a reference to the code.
When a subroutine is called, the statements in block are executed. Parameters are passed as a flat list of scalars as array @_
. The elements of @_
are aliases for the scalar parameters. The call returns the value of the last expression evaluated. wantarray can be used to determine the context in which the subroutine was called.
Subroutines that have an empty prototype and do nothing but return a fixed value are inlined, e.g., sub PI() { 3.1415 }
.
attributes are introduced with a :
(colon). The following attributes are currently implemented:
| The subroutine is a method. |
| Lock this subroutine against concurrent access. |
| The subroutine returns a variable that can be assigned to. |
There are several ways to call a subroutine.
(
[ parameters ] )
The most common way. The parameters are passed by reference as array @_
.
(
[ parameters ] )
Prototype specifications, if any, are ignored.
The current @_
is passed directly to the called subroutine.
If the subroutine has been declared, or defined, it may be called as a built-in operator, without parentheses.
In all cases, name can be an expression yielding a reference to a code object. If so, you can also use &$
{expr}( [ arguments ] )
or $
{expr}->(
[arguments] )
.
Returns a list (package, file, line, . . . ) for a specific subroutine call. caller
returns this information for the current subroutine, caller(1)
returns this information for the subroutine that called this subroutine, etc. Returns false if no caller.
Tests whether the named subroutine has been defined (has a body).
Deprecated form of &
name.
Tests whether the named subroutine has been declared, either in full (with a body) or with a forward declaration.
&
nameSubstitutes a call to name for the current subroutine.
Returns the prototype for the named function as a string, or undef if the function has no usable prototype.
Returns from a subroutine, eval, or do file with the value specified. Without expr, returns undef in scalar context and an empty list in list context.
Special subroutines are user defined, but are called by Perl while processing the program. They can be used to change the order in which parts of a program are executed.
The code in block is executed when the program calls an undefined subroutine. $AUTOLOAD
contains the name of the called subroutine, and @_
contains the parameters.
The code in block is executed immediately when compilation of the block is complete.
CHECK blocks are executed in reverse order when the compilation of the program finishes.
END blocks are executed in reverse order when the Perl interpreter terminates. Inside the END blocks, $?
contains the status with which the program is going to exit.
INIT blocks are executed immediately before the Perl interpreter starts executing the program.