Basic syntax
In Perl, the minimal Hello World program may be written as follows:n
character whose interpretation is altered by the preceding perl
. (Note that, on .pl
perl
detects the shebang line and parses it for switches.)
The second line in the canonical form includes a semicolon, which is used to separate statements in Perl. With only a single statement in a block or file, a separator is unnecessary, so it can be omitted from the minimal form of the program—or more generally from the final statement in any block or file. The canonical form includes it, because it is common to terminate every statement even when it is unnecessary to do so, as this makes editing easier: code can be added to, or moved away from, the end of a block or file without having to adjust semicolons.
Version 5.10 of Perl introduces a say
function that implicitly appends a newline character to its output, making the minimal "Hello World" program even shorter:
Data types
Perl has a number of fundamentalScalar values
String values (literals) must be enclosed by quotes. Enclosing a string in double quotes allows the values of variables whose names appear in the string to automatically replace the variable name (or be interpolated) in the string. Enclosing a string in single quotes prevents variable interpolation. For example, if$name
is "Jim"
:
*then print("My name is $name")
will print "My name is Jim"
(interpolation within double quotes),
*but print('My name is $name')
will print "My name is $name"
(no interpolation within single quotes).
To include a double quotation mark in a string, precede it with a backslash or enclose the string in single quotes. To include a single quotation mark, precede it with a backslash or enclose the string in double quotes.
Strings can also be quoted with the q
and qq
quote-like operators:
*'this'
and q(this)
are identical,
*"$this"
and qq($this)
are identical.
Finally, multiline strings can be defined using here documents:
$n
and $m
are treated as numbers. This code prints the number '5'. The values of the variables remain the same. Note that in Perl, +
is always the numeric addition operator. The string concatenation operator is the period.
int
chops off the fractional part, rounding towards zero; POSIX::ceil
and POSIX::floor
round always up and always down, respectively. The number-to-string conversion of printf "%f"
or sprintf "%f"
round out even, use bankers' rounding.
Perl also has a boolean context that it uses in evaluating conditional statements. The following values all evaluate as false in Perl:
is also true; in this context
is not an empty block, because perl -e 'print ref '
returns HASH
.
Evaluated boolean expressions are also scalar values. The documentation does not promise which ''particular'' value of true or false is returned. Many boolean operators return 1 for true and the empty-string for false. The ''defined()'' function determines whether a variable has any value set. In the above examples, ''defined($false)'' is true for every value except ''undef''.
If either 1 or 0 are specifically needed, an explicit conversion can be done using the conditional operator:
Array values
An array value (or list) is specified by listing its elements, separated by commas, enclosed by parentheses (at least where required by operator precedence).$month /code> is "April"
(the first element in an array has an index value of 0), and @month ..6/code> is ("May", "June", "July")
.
Hash values
Perl programmers may initialize a hash (or associative array) from a list of key/value pairs. If the keys are separated from the values with the =>
operator (sometimes called a fat comma
The fat comma (also termed hash rocket in Ruby and a fat arrow in JavaScript) is a syntactic construction that appears in a position in a function call (or definition) where a comma would usually appear. The original usage refers to the ")''lette ...
), rather than a comma, they may be unquoted (barewords). The following lines are equivalent:
%favorite = ('joe', "red", 'sam', "blue");
%favorite = (joe => 'red', sam => 'blue');
Individual values in a hash are accessed by providing the corresponding key, in curly braces. The $
sigil identifies the accessed element as a scalar. For example, $favorite equals 'red'. A hash can also be initialized by setting its values individually:
$favorite = 'red';
$favorite = 'blue';
$favorite = 'green';
Multiple elements may be accessed using the @
sigil instead (identifying the result as a list). For example,
@favorite equals ('red', 'blue').
Filehandles
Filehandles provide read and write access to resources. These are most often files on disk, but can also be a device, a pipe, or even a scalar value.
Originally, filehandles could only be created with package variables, using the ALL_CAPS convention to distinguish it from other variables. Perl 5.6 and newer also accept a scalar variable, which will be set ( autovivified) to a reference to an anonymous filehandle, in place of a named filehandle.
Typeglob values
A typeglob value is a symbol table entry. The main use of typeglobs is creating symbol table aliases. For example:
*PI = \3.141592653; # creating constant scalar $PI
*this = *that; # creating aliases for all data types 'this' to all data types 'that'
Array functions
The number of elements in an array can be determined either by evaluating the array in scalar context or with the help of the $#
sigil. The latter gives the index of the last element in the array, not the number of elements. The expressions scalar(@array) and ($#array + 1) are equivalent.
Hash functions
There are a few functions that operate on entire hashes. The ''keys'' function takes a hash and returns the list of its keys. Similarly, the ''values'' function returns a hash's values. Note that the keys and values are returned in a consistent but arbitrary order.
# Every call to each returns the next key/value pair.
# All values will be eventually returned, but their order
# cannot be predicted.
while (($name, $address) = each %addressbook)
# Similar to the above, but sorted alphabetically
foreach my $next_name (sort keys %addressbook)
Control structures
Perl has several kinds of control structures.
It has block-oriented control structures, similar to those in the C, JavaScript, and Java programming languages. Conditions are surrounded by parentheses, and controlled blocks are surrounded by braces:
''label'' while ( ''cond'' )
''label'' while ( ''cond'' ) continue
''label'' for ( ''init-expr'' ; ''cond-expr'' ; ''incr-expr'' )
''label'' foreach ''var'' ( ''list'' )
''label'' foreach ''var'' ( ''list'' ) continue
if ( ''cond'' )
if ( ''cond'' ) else
if ( ''cond'' ) elsif ( ''cond'' ) else
Where only a single statement is being controlled, statement modifiers provide a more-concise syntax:
''statement'' if ''cond'' ;
''statement'' unless ''cond'' ;
''statement'' while ''cond'' ;
''statement'' until ''cond'' ;
''statement'' foreach ''list'' ;
Short-circuit logical operators are commonly used to affect control flow at the expression level:
''expr'' and ''expr''
''expr'' && ''expr''
''expr'' or ''expr''
''expr'' , , ''expr''
(The "and" and "or" operators are similar to && and , , but have lower precedence
Precedence may refer to:
* Message precedence of military communications traffic
* Order of precedence, the ceremonial hierarchy within a nation or state
* Order of operations, in mathematics and computer programming
* Precedence Entertainment, a ...
, which makes it easier to use them to control entire statements.)
The flow control keywords next
(corresponding to C's continue
), last
(corresponding to C's break
), return
, and redo
are expressions, so they can be used with short-circuit operators.
Perl also has two implicit looping constructs, each of which has two forms:
''results'' = grep ''list''
''results'' = grep ''expr'', ''list''
''results'' = map ''list''
''results'' = map ''expr'', ''list''
grep
returns all elements of ''list'' for which the controlled block or expression evaluates to true. map
evaluates the controlled block or expression for each element of ''list'' and returns a list of the resulting values. These constructs enable a simple functional programming style.
Up until the 5.10.0 release, there was no switch statement
In computer programming languages, a switch statement is a type of selection control mechanism used to allow the value of a variable or expression to change the control flow of program execution via search and map.
Switch statements function some ...
in Perl 5. From 5.10.0 onward, a multi-way branch statement called given
/when
is available, which takes the following form:
use v5.10; # must be present to import the new 5.10 functions
given ( ''expr'' )
Syntactically, this structure behaves similarly to switch statement
In computer programming languages, a switch statement is a type of selection control mechanism used to allow the value of a variable or expression to change the control flow of program execution via search and map.
Switch statements function some ...
s found in other languages, but with a few important differences. The largest is that unlike switch/case structures, given/when statements break execution after the first successful branch, rather than waiting for explicitly defined break commands. Conversely, explicit continue
s are instead necessary to emulate switch behavior.
For those not using Perl 5.10, the Perl documentation describes a half-dozen ways to achieve the same effect by using other control structures. There is also a Switch module, which provides functionality modeled on that of sister language Raku. It is implemented using a source filter, so its use is unofficially discouraged.
Perl includes a goto label
statement, but it is rarely used. Situations where a goto
is called for in other languages don't occur as often in Perl, because of its breadth of flow control options.
There is also a goto &sub
statement that performs a tail call. It terminates the current subroutine and immediately calls the specified ''sub''
. This is used in situations where a caller can perform more-efficient stack
Stack may refer to:
Places
* Stack Island, an island game reserve in Bass Strait, south-eastern Australia, in Tasmania’s Hunter Island Group
* Blue Stack Mountains, in Co. Donegal, Ireland
People
* Stack (surname) (including a list of people ...
management than Perl itself (typically because no change to the current stack is required), and in deep recursion, tail calling can have substantial positive impact on performance, because it avoids the overhead of scope/stack management on return.
Subroutines
Subroutine
In computer programming, a function or subroutine is a sequence of program instructions that performs a specific task, packaged as a unit. This unit can then be used in programs wherever that particular task should be performed.
Functions may ...
s are defined with the sub
keyword and are invoked simply by naming them. If the subroutine in question has not yet been declared, invocation requires either parentheses after the function name or an ampersand (&) before it. But using & without parentheses will also implicitly pass the arguments of the current subroutine to the one called, and using & with parentheses will bypass prototypes.
# Calling a subroutine
# Parentheses are required here if the subroutine is defined later in the code
foo();
&foo; # (this also works, but has other consequences regarding arguments passed to the subroutine)
# Defining a subroutine
sub foo
foo; # Here parentheses are not required
A list of arguments may be provided after the subroutine name. Arguments may be scalars, lists, or hashes.
foo $x, @y, %z;
The parameters to a subroutine do not need to be declared as to either number or type; in fact, they may vary from call to call. Any validation of parameters must be performed explicitly inside the subroutine.
Arrays are expanded to their elements; hashes are expanded to a list of key/value pairs; and the whole lot is passed into the subroutine as one flat list of scalars.
Whatever arguments are passed are available to the subroutine in the special array @_
. The elements of @_
are references to the actual arguments; changing an element of @_
changes the corresponding argument.
Elements of @_
may be accessed by subscripting it in the usual way.
$_ $_
However, the resulting code can be difficult to read, and the parameters have pass-by-reference
In a programming language, an evaluation strategy is a set of rules for evaluating expressions. The term is often used to refer to the more specific notion of a ''parameter-passing strategy'' that defines the kind of value that is passed to the f ...
semantics, which may be undesirable.
One common idiom is to assign @_
to a list of named variables.
my ($x, $y, $z) = @_;
This provides mnemonic parameter names and implements pass-by-value semantics. The my
keyword indicates that the following variables are lexically scoped to the containing block.
Another idiom is to shift parameters off of @_
. This is especially common when the subroutine takes only one argument or for handling the $self
argument in object-oriented modules.
my $x = shift;
Subroutines may assign @_
to a hash to simulate named arguments; this is recommended in ''Perl Best Practices
''Perl Best Practices'' is a programming book focusing on standard practices for Perl coding style, encouraging the development of maintainable source code. It was written by Damian Conway and published by O'Reilly.
References
External links
*P ...
'' for subroutines that are likely to ever have more than three parameters.
sub function1
function1( x => 23 );
Subroutines may return values.
return 42, $x, @y, %z;
If the subroutine does not exit via a return
statement, it returns the last expression evaluated within the subroutine body. Arrays and hashes in the return value are expanded to lists of scalars, just as they are for arguments.
The returned expression is evaluated in the calling context of the subroutine; this can surprise the unwary.
sub list
sub array
$x = list; # returns 6 - last element of list
$x = array; # returns 3 - number of elements in list
@x = list; # returns (4, 5, 6)
@x = array; # returns (4, 5, 6)
A subroutine can discover its calling context with the wantarray
function.
sub either
$x = either; # returns "Oranges"
@x = either; # returns (1, 2)
Regular expressions
The Perl language includes a specialized syntax for writing regular expressions (RE, or regexes), and the interpreter contains an engine for matching strings to regular expressions. The regular-expression engine uses a backtracking algorithm, extending its capabilities from simple pattern matching to string capture and substitution. The regular-expression engine is derived from regex written by Henry Spencer.
The Perl regular-expression syntax was originally taken from Unix Version 8 regular expressions. However, it diverged before the first release of Perl and has since grown to include far more features. Many other languages and applications are now adopting Perl Compatible Regular Expressions over POSIX regular expressions, such as PHP, Ruby, Java, Microsoft's .NET Framework
The .NET Framework (pronounced as "''dot net"'') is a proprietary software framework developed by Microsoft that runs primarily on Microsoft Windows. It was the predominant implementation of the Common Language Infrastructure (CLI) until bein ...
, and the Apache HTTP server
The Apache HTTP Server ( ) is a free and open-source cross-platform web server software, released under the terms of Apache License 2.0. Apache is developed and maintained by an open community of developers under the auspices of the Apache So ...
.
Regular-expression syntax is extremely compact, owing to history. The first regular-expression dialects were only slightly more expressive than globs, and the syntax was designed so that an expression would resemble the text that it matches. This meant using no more than a single punctuation character or a pair of delimiting characters to express the few supported assertions. Over time, the expressiveness of regular expressions grew tremendously, but the syntax design was never revised and continues to rely on punctuation. As a result, regular expressions can be cryptic and extremely dense.
Uses
The m//
(match) operator introduces a regular-expression match. (If it is delimited by slashes, as in all of the examples here, the leading m
may be omitted for brevity. If the m
is present, as in all of the following examples, other delimiters can be used in place of slashes.) In the simplest case, an expression such as
$x =~ /abc/;
evaluates to true if and only if the string $x
matches the regular expression abc
.
The s///
(substitute) operator, on the other hand, specifies a search-and-replace operation:
$x =~ s/abc/aBc/; # upcase the b
Another use of regular expressions is to specify delimiters for the split
function:
@words = split /,/, $line;
The split
function creates a list of the parts of the string that are separated by what matches the regular expression. In this example, a line is divided into a list of its own comma-separated parts, and this list is then assigned to the @words
array.
Syntax
Modifiers
Perl regular expressions can take ''modifiers''. These are single-letter suffixes that modify the meaning of the expression:
$x =~ /abc/i; # case-insensitive pattern match
$x =~ s/abc/aBc/g; # global search and replace
Because the compact syntax of regular expressions can make them dense and cryptic, the /x
modifier was added in Perl to help programmers write more-legible regular expressions. It allows programmers to place whitespace and comments ''inside'' regular expressions:
$x =~ /
a # match 'a'
. # followed by any character
c # then followed by the 'c' character
/x;
Capturing
Portions of a regular expression may be enclosed in parentheses; corresponding portions of a matching string are ''captured''. Captured strings are assigned to the sequential built-in variables $1, $2, $3, …
, and a list of captured strings is returned as the value of the match.
$x =~ /a(.)c/; # capture the character between 'a' and 'c'
Captured strings $1, $2, $3, …
can be used later in the code.
Perl regular expressions also allow built-in or user-defined functions to apply to the captured match, by using the /e
modifier:
$x = "Oranges";
$x =~ s/(ge)/uc($1)/e; # OranGEs
$x .= $1; # append $x with the contents of the match in the previous statement: OranGEsge
Objects
There are many ways to write object-oriented code in Perl. The most basic is using "blessed" references
Reference is a relationship between objects in which one object designates, or acts as a means by which to connect to or link to, another object. The first object in this relation is said to ''refer to'' the second object. It is called a ''name'' ...
. This works by identifying a reference of any type as belonging to a given package, and the package provides the methods for the blessed reference. For example, a two-dimensional point could be defined this way:
sub Point::new
sub Point::distance
This class can be used by invoking new()
to construct instances, and invoking distance
on those instances.
my $p1 = Point->new(3, 4);
my $p2 = Point->new(0, 0);
print $p1->distance($p2); # Prints 5
Many modern Perl applications use the Moose object system. Moose is built on top of Class::MOP, a meta-object protocol, providing complete introspection for all Moose-using classes. Thus you can ask classes about their attributes, parents, children, methods, etc. using a simple API.
Moose classes:
* A class has zero or more attributes.
* A class has zero or more methods.
* A class has zero or more superclasses (aka parent classes). A class inherits from its superclass(es).
* A class does zero or more roles, which add the ability to add pre-defined functionality to classes without subclassing.
* A class has a constructor and a destructor.
* A class has a metaclass.
* A class has zero or more method modifiers. These modifiers can apply to its own methods, methods that are inherited from its ancestors, or methods that are provided by roles.
Moose roles:
* A role is something that a class does, somewhat like mixins or interfaces in other object-oriented programming languages. Unlike mixins and interfaces, roles can be applied to individual object instances.
* A role has zero or more attributes.
* A role has zero or more methods.
* A role has zero or more method modifiers.
* A role has zero or more required methods.
Examples
An example of a class written using the MooseX::DeclareMooseX::Declare documentation
/ref> extension to Moose:
use MooseX::Declare;
class Point3D extends Point
This is a class named Point3D
that extends another class named Point
explained in Moose examples. It adds to its base class a new attribute z
, redefines the method set_to
and extends the method clear
.
References
{{reflist
External links
Perl tutorials
PerlMonks
A community committed to sharing Perl knowledge and coding tips.
Articles with example Perl code
Perl