O'Reilly logo

Learning Perl, Second Edition by Randal L. Schwartz, Tom Christiansen

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

4.1. Statement Blocks

A statement block is a sequence of statements, enclosed in matching curly braces. It looks like this:

{
    first_statement;
    second_statement;
    third_statement;
    ...
    last_statement;
}

Perl executes each statement in sequence, from the first to the last. (Later, I'll show you how to alter this execution sequence within a block, but this is good enough for now.)

Syntactically, a block of statements is accepted in place of any single statement, but the reverse is not true.

The final semicolon on the last statement is optional. Thus, you can speak Perl with a C-accent (semicolon present) or Pascal-accent (semicolon absent). To make it easier to add more statements later, we usually suggest omitting the semicolon only when the block is all on one line. Contrast these two if blocks for examples of the two styles:

if ($ready) { $hungry++ }
    if ($tired) {
        $sleepy = ($hungry + 1) * 2;
}

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required