Perl sub Function
This function defines a new subroutine. The arguments shown above follow these rules −
NAME is the name of the subroutine. Named subroutines can be predeclared (without an associated code block) with, or without, prototype specifications.
Anonymous subroutines must have a definition.
PROTO defines the prototype for a function, which will be used when the function is called to validate the supplied arguments.
ATTRS define additional information for the parser about the subroutine being declared.
Following is the simple syntax for this function −
sub NAME PROTO ATTRS BLOCK# Named, prototype, attributes, definition sub NAME ATTRS BLOCK # Named, attributes, definition sub NAME PROTO BLOCK # Named, prototype, definition sub NAME BLOCK # Named, definition sub NAME PROTO ATTRS # Named, prototype, attributes sub NAME ATTRS # Named, attributes sub NAME PROTO # Named, prototype sub NAME # Named sub PROTO ATTRS BLOCK # Anonymous, prototype, attributes, definition sub ATTRS BLOCK # Anonymous, attributes, definition sub PROTO BLOCK # Anonymous, prototype, definition sub BLOCK # Anonymous, definition
This function does not return any value.
Following is the example code showing its basic usage −
When above code is executed, it produces the following result