home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  

Learning Perl

Learning PerlSearch this book
Previous: 3.8 Exercises Chapter 4 Next: 4.2 The if/unless Statement

4. Control Structures

4.1 Statement Blocks

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


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;