Subroutines

Subroutines need a declaration, i.e., a specification of how they should be called, and a definition, i.e., a specification of what they should do when called.

sub name [ ( 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.

sub [ name ] [ ( proto ) ] [ attributes ] block

Defines 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 (page 11) 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 }. See also the subsection Prototypes.

attributes are introduced with a : (colon). Perl supports the following attributes:

lvalue

The subroutine returns a scalar variable that can be assigned to.

method

The subroutine is a method.

There are several ways to call a subroutine.

name ( [ parameters ] )

The most common way. The parameters are passed by reference as array @_.

&name ( [ parameters ] )

Prototype specifications, if any, are ignored.

&name

The current @_ is passed directly to the subroutine.

name [ arguments ]

If the subroutine has been declared, or defined, it may be called as a built-in operator, without parentheses.

caller [ expr ]

Returns a list (package, file, line) for a specific subroutine call. caller returns this information for the current subroutine, With expr, returns an extended list; caller(0) for this subroutine, caller(1) for the subroutine that called this subroutine, etc. See the table on the following page.

Returns false if no caller.

Elements of the extended result list:

Index

Name

Description

0

package

The calling package.

1

filename

The filename.

2

line

The line number.

3

subroutine

The name of the subroutine.

4

hasargs

True if arguments were supplied.

5

wantarray

Call context.

6

evaltext

Text if called by eval.

7

is_require

Called by use or require.

8

hints

Pragmatic hints.

9

bitmask

Pragmatic hints.

10

hinthash

Pragmatic hints.

defined &name

Tests whether the named subroutine has been defined (has a body).

do name list

Deprecated form of &name.

exists &name

True if the named subroutine has been declared, either with a body or with a forward declaration.

goto &name

Substitutes a call to name for the current subroutine.

return [ expr ]

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.

perlsub, perlmod.

Prototypes

Proto

Meaning

$

Enforces scalar context.

@ %

Enforce list context; all remaining arguments are consumed.

*

Accepts a bare name, a constant, a scalar expression, a typeglob, or a reference to a typeglob.

&

Requires a code (subroutine) reference.

$

The argument must start with a $.

@ \%

The argument must start with a @, a %.

* &

The argument must start with a *, a &.

[...]

The argument must start with any of the specified sigils.

 

(Empty parentheses) If the subroutine returns a fixed value the call is inlined.

_

Requires a scalar expression, defaults to $_.

+

Requires an array, a hash, or a reference.

;

Indicates that subsequent arguments are optional.

prototype name

Returns the prototype for the named function as a string, or undef if the function has none.

Use CORE::name for built-in functions.

perlsub, perlmod.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset