next up previous contents
Next: Understanding Expressions Up: Practical Perl Programming Previous: Order of Precedence

Perl Statements

  If you look at a Perl program from a very high level, it is made of statements. Statements are a complete unit of instruction for the computer to process. The computer executes each statement it sees-in sequence-until a jump or branch is processed.

Statements can be very simple or very complex. The simplest statement is this

123;

which is a numeric literal followed by a semicolon. The semicolon is very important. It tells Perl that the statement is complete. A more complicated statement might be

$bookSize = ($numOfPages >= 1200 ?  "Large" : "Normal");

which says if the number of pages is 1,200 or greater, then assign "Large" to $bookSize; otherwise, assign "Normal" to $bookSize.

In Perl, every statement has a value. In the first example, the value of the statement is 123.

In the second example, the value of the statement could be either "Large" or "Normal" depending on the value of $numOfPages. The last value that is evaluated becomes the value for the statement.

Like human language in which you put statements together from parts of speech-nouns, verbs, and modifiers-you can also break down Perl statements into parts. The parts are the literals, variables, and functions you have already seen in the earlier chapters of this book.

Expressions are a sequence of literals, variables, and functions connected by one or more operators that evaluate to a single value-scalar or array. An expression can be promoted to a statement by adding a semicolon. This was done for the first example earlier. Simply adding a semicolon to the literal made it into a statement that Perl could execute.

Expressions may have side effects, also. Functions that are called can do things that are not immediately obvious (like setting global variables) or the pre- and post-increment operators can be used to change a variable's value.

Let's take a short diversion from our main discussion about statements and look at expressions in isolation. Then we'll return to statements to talk about statement blocks and statement modifiers.


 
next up previous contents
Next: Understanding Expressions Up: Practical Perl Programming Previous: Order of Precedence
dave@cs.cf.ac.uk