CHAPTER 7Subroutines

Subroutines are autonomous blocks of code that function like miniature programs. They can be executed from anywhere within a program, and because they are autonomous, calling them more than once will also reuse them.

There are two types of subroutine, named and anonymous. Most subroutines are of the named persuasion, called by writing their names literally into the source code. Anonymous subroutines do not have a name by which they can be called but are stored and accessed through a code reference. Since a code reference is a scalar value, it can be passed as a parameter to other subroutines and built-in functions like sort.

The use of named subroutines is syntactically the same as the use of Perl's own built-in functions. ...

Get Pro Perl 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.