Blocks

The simplest grouping of statements in Perl is called a block. To group statements in a block, you can simply surround the statements with a matched set of braces, as shown here:

{
    statement_a;
						statement_b;
						statement_c;
}

Within the block, statements execute from the top down, as they have until now. Within a block, you can have other blocks of statements, as you can see here:

{
    statement_a;
    {
        statement_x;
						statement_y;
    }
}

The format of the block, like the rest of Perl, is free-form. The statements and curly braces can be on one line or on several lines, as shown here, and with any kind of alignment you want as long as you always have a matched set of curly braces:

{ statement;  { another_statement; }
       { last_statement;        } }

Although ...

Get Sams Teach Yourself Perl in 24 Hours 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.