Skip to Main Content
Perl Pocket Reference, 5th Edition
book

Perl Pocket Reference, 5th Edition

by Johan Vromans
July 2011
Intermediate to advanced content levelIntermediate to advanced
104 pages
2h 14m
English
O'Reilly Media, Inc.
Content preview from Perl Pocket Reference, 5th Edition

Statements

A statement is an expression, optionally followed by a modifier, and terminated with a semicolon. Statements can be combined to form a block when enclosed in {}. The semicolon may be omitted after the last statement of a block.

Execution of expressions can depend on other expressions using one of the modifiers if, unless, for, foreach, when, while, or until, for example:

  • expr1 if expr2 ;

  • expr1 foreach list ;

The operators ||, //, &&, or ?: also allow conditional execution:

  • expr1 || expr2 ;

  • expr1 ? expr2 : expr3 ;

Blocks may be used for conditional execution:

  • if ( expr ) block [ elsif ( expr ) block . . . ] [ else block ]

  • unless ( expr ) block [ else block ]

Loop blocks

  • [ label: ] while (expr) block [ continue block ]

  • [ label: ] until (expr) block [ continue block ]

  • [ label: ] for ( [ expr ] ; [ expr ] ; [ expr ] ) block

  • [ label: ] foreach var(list) block [ continue block ]

  • [ label: ] block [ continue block ]

In foreach, the iteration variable (default $_) is aliased to each element of the list, so modifying this variable modifies the actual list element.

The keywords for and foreach can be used interchangeably.

In loop blocks, program flow can be controlled with:

goto label

Finds the statement labeled with label and resumes execution there. label may be an expression that evaluates to the name of a label.

last [ label ]

Immediately exits the loop. Skips the continue block.

next [ label ]

Executes the continue block and starts the next iteration of the loop.

redo [ label ]

Restarts the loop block ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Perl Testing: A Developer's Notebook

Perl Testing: A Developer's Notebook

Ian Langworth, Chromatic
Perl Hacks

Perl Hacks

Chromatic, Damian Conway, Curtis Ovid Poe, Curtis (Ovid) Poe

Publisher Resources

ISBN: 9781449311186Errata Page