Anonymous Functions
Learn about anonymous functions in Perl.
We'll cover the following...
We'll cover the following...
An anonymous function is a function without a name. It behaves exactly like a named function—we can invoke it, pass it arguments, return values from it, and take references to it. Yet, we can access an anonymous function only by reference, not by name.
A Perl idiom known as a dispatch table uses hashes to associate input with behavior:
The dispatch() function takes arguments of the form (2, 'times', 2), evaluates the operation, and returns the result. A trivial calculator application could use
dispatch to figure out which calculation to perform based on user input.
Declaring anonymous functions
The sub built-in used without a name creates and returns an anonymous
function. We should use this function ...