< Perl Programming < Keywords

The sub keyword

The sub keyword defines a subroutine. Without a BLOCK, it is a forward declaration. By an assignment larger or shorter than LENGTH, the string will grow or shrink respectively to accommodate it. Without NAME, it defines an anonymous function, but returns the CODE ref of the just created closure.

Syntax

  sub NAME BLOCK
  sub NAME (PROTO) BLOCK
  sub NAME : ATTRIBUTES BLOCK
  sub NAME (PROTO) : ATTRIBUTES BLOCK

Examples

sub { $x = 1; };
sub sam { ... };
This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.