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

Book HomeJava and XSLTSearch this book

4.3. Statements

A simple statement is an expression evaluated for its side effects. Every simple statement must end in a semicolon, unless it is the final statement in a block.

A sequence of statements that defines a scope is called a block. Generally, a block is delimited by braces, or { }. Compound statements are built out of expressions and blocks. A conditional expression is evaluated to determine whether a statement block will be executed. Compound statements are defined in terms of blocks, not statements, which means that braces are required.

Any block can be given a label. Labels are identifiers that follow the variable-naming rules (i.e., they begin with a letter or underscore and can contain alphanumerics and underscores). They are placed just before the block and are followed by a colon, such as SOMELABEL here:


By convention, labels are all uppercase, so as not to conflict with reserved words. Labels are used with the loop control commands next, last, and redo to alter the flow of execution in your programs.

4.3.1. Conditionals and Loops

The if and unless statements execute blocks of code depending on whether a condition is met. These statements take the following forms:

if (expression) {block} else {block}

unless (expression) {block} else {block}

if (expression1) {block}
elsif (expression2) {block}
elsif (lastexpression) {block}
else {block} goto

Perl supports a goto command. There are three forms: goto label, goto expr, and goto &name. In general, you shouldn't need to use goto, which you'll conclude if you do a search for goto in the comp.lang.perl.misc archives on groups.google.com.

The goto label form finds the statement labeled with label and resumes execution there. It may not be used to go inside any construct that requires initialization, such as a subroutine or a foreach loop.

The goto expr form expects the expression to return a label name.

The goto &name form substitutes a call to the named subroutine for the currently running subroutine.

Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.