Calling Subroutines

The ampersand (&) is the identifier used to call subroutines. Most of the time, however, subroutines can be used in an expression just like built-in functions. To call subroutines directly:

                  name(args);        # & is optional with parentheses
name 
                  args;         # Parens optional if predeclared/imported
&name;             # Passes current @_ to subroutine

To call subroutines indirectly (by name or by reference):

&$subref(args);    # & is not optional on indirect call
&$subref;          # Passes current @_ to subroutine

Get Perl in a Nutshell, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.