Perl sub Function

Advertisements


Description

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.

Syntax

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

Return Value

This function does not return any value.

Example

Following is the example code showing its basic usage:


When above code is executed, it produced following result




Advertisements
Advertisements