A prototype is a piece of metadata attached to a function or variable. A function prototype changes how Perl’s parser understands it.

Prototypes allow us to define our own functions that behave like built-ins. Consider the built-in push, which takes an array and a list. While Perl would normally flatten the array and list into a single list passed to push, Perl knows to treat the array as a container and doesn't flatten its values. In effect, this is like passing a reference to an array and a list of values to push—because Perl’s parser understands this is what push needs to do.

Function prototypes

Function prototypes attach to function declarations.

Any prototype attached to a forward declaration must match the prototype attached to the function declaration. Perl will give a warning if this isn’t true. Strangely, we may omit the prototype from a forward declaration and include it for the full declaration—but the only reason to do so is to win a trivia contest:

Get hands-on with 1200+ tech skills courses.