Chapter 4. Statements and Declarations

A Perl program consists of a sequence of declarations and statements. A declaration may be placed anywhere a statement may be placed, but its primary effect occurs at compile time. A few declarations do double duty as ordinary statements, but most are totally transparent at run time. After compilation, the main sequence of statements is executed just once.

Unlike many programming languages, Perl doesn't require variables to be explicitly declared; they spring into existence upon their first use, whether you've declared them or not. If you try to use a value from a variable that's never had a value assigned to it, it's quietly treated as 0 when used as a number, as "" (the null string) when used as a string, or simply as false when used as a logical value. If you prefer to be warned about using undefined values as though they were real strings or numbers, or even to treat doing so as an error, the use warnings declaration will take care of that; see the section Section 4.9 at the end of this chapter.

You may declare your variables though, if you like, using either my or our in front of the variable name. You can even make it an error to use an undeclared variable. This kind of discipline is fine, but you have to declare that you want the discipline. Normally, Perl minds its own business about your programming habits, but under the use strict declaration, the use of undeclared variables is apprehended at compile time. Again, see Section 4.9.

Simple Statements

A simple statement is an expression evaluated for its side effects. Every simple statement must end in a semicolon, unless it is the final statement in a block. In that case, the semicolon is optional--Perl knows that you must be done with the statement, since you've finished the block. But put the semicolon in anyway if it's at the end of a multiline block, because you might eventually add another line.

Even though operators like eval {}, do {}, and sub {} all look like compound statements, they really aren't. True, they allow multiple statements on the inside, but that doesn't count. From the outside, those operators are just terms in an expression, and thus they need an explicit semicolon if used as the last item in a statement.

Any simple statement may optionally be followed by a single modifier, just before the terminating semicolon (or block ending). The possible modifiers are:

if EXPR
unless EXPR
while EXPR
until EXPR
foreach LIST

The if and unless modifiers work pretty much as they do in English:

$trash->take('out') if $you_love_me;
shutup() unless $you_want_me_to_leave;

The while and until modifiers evaluate repeatedly. As you might expect, a while modifier keeps executing the expression as long as its expression remains true, and an until modifier keeps executing only as long as it remains false:

$expression++ while -e "$file$expression";
kiss('me') until $I_die;

The foreach modifier (also spelled for) evaluates once for each element in its LIST, with $_ aliased to the current element:

s/java/perl/ for @resumes;
print "field: $_\n" foreach split /:/, $dataline;

The while and until modifiers have the usual while-loop semantics (conditional evaluated first), except when applied to a do BLOCK (or to the now-deprecated do SUBROUTINE statement), in which case the block executes once before the conditional is evaluated. This allows you to write loops like this:

do {
    $line = <STDIN>;
    …
} until $line eq ".\n";

See the three different do entries in Chapter 29. Note also that the loop-control operators described later will not work in this construct, since modifiers don't take loop labels. You can always place an extra block around it to terminate early, or inside it to iterate early, as described later in Section 4.5. Or you could write a real loop with multiple loop controls inside. Speaking of real loops, we'll talk about compound statements next.

Get Programming Perl, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.