Chapter 7. Subroutines
Like many languages, Perl provides for user-defined subroutines.[115] These subroutines may be defined anywhere in the main
program, loaded in from other files via the do, require, or use keywords, or generated at runtime using
eval. You can even load them at
runtime with the mechanism described in the section Autoloading in Chapter 10. You can call a
subroutine indirectly, using a variable containing either its name or a
reference to the routine, or through an object, letting the object
determine which subroutine should really be called. You can generate
anonymous subroutines, accessible only through references, and if you
want, use these to clone new, nearly identical functions via
closures (which are covered in the section by that
name in Chapter 8).
[115] We’ll also call them functions, but functions are the same thing as subroutines in Perl. Sometimes we’ll even call them methods, which are defined the same way, but called differently.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access