Приглашаем посетить
Древнерусская литература (drevne-rus-lit.niv.ru)

Blocks

Previous Table of Contents Next

Blocks

The fundamental way to group statements in Perl is the block. To group statements in a block, just surround the statements with a matched set of curly braces, as shown here:


{

    statement_a;

    statement_b;

    statement_c;

}


Within the block, statements execute from the top down, as they have until now. You can have other, smaller blocks of statements nested within a block, 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 you can arrange blocks any way you would like, programs can be hard to read if they're just thrown together. Good indenting, although not necessary for Perl to understand your code, makes for human-readable Perl. It can help you keep track of your program's logic.

Blocks that occur by themselves within a program are called bare blocks or naked blocks. Most of the time, however, you will encounter blocks attached to other Perl statements.

    Previous Table of Contents Next