Perl for Newbies - Part 3 - Modules and ObjectsIntroductionReferences to Functions

1.1. References to Functions

Perl allows the programmer to store a reference to a function inside a scalar value. This variable can later be dereferenced in order to invoke the function with any number of arguments.

By using this mechanism, one can implement callbacks, and make sure a wrapper function can invoke several helper functions which it will treat the same.


In perl it is possible to define dynamic subroutines, inside the code of the current scope. That scope can be a function or even another dynamic subroutine. These subroutines, which are sometimes referred to as closures, can see all the variables of the scope in which they were declared, even after the function that created them stopped running.

Closures enable the program to pass state information into callbacks that do not accept state information, and are generally a very convenient mechanism.

Written by Shlomi Fish