Complex Statements and Blocks

Conditionals and loops are sometimes called complex statements. That's because instead of being a single statement like $x = 5 or $array[5] = "fifth" ending with a semicolon, conditionals and loops tend to be more, well, complex. Probably the biggest difference between simple and complex statements, however, is that the latter operate on chunks of Perl code called blocks.

A block is, simply, a group of any Perl statements surrounded by curly braces ({}). Inside the block, you can include other statements, other blocks, or anything that can appear outside the block. As with statements in a script, too, the statements inside the block are executed in order. For example, here are two blocks, one that is associated with ...

Get Sams Teach Yourself Perl in 21 Days, Second 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.