next up previous contents
Next: Statement Blocks Up: Perl Statements Previous: Perl Statements

Understanding Expressions

You can break the universe of expressions up into four types:

Simple expressions consist of a single literal or variable.

Simple expressions with side effects -- A side effect is when a variable's value is changed by the expression. Side effects can be caused using any of the unary operators: +, -, ++, -. These operators have the effect of changing the value of a variable just by the evaluation of the expression.No other Perl operators have this effect-other than the assignment operators, of course. Function calls can also have side effects- especially if local variables were not used and changes were made to global variables.

For example:

$numPages++; # Increments a variable

++$numPages; # Increments a variable
 
chop($firstVar); # Changes the value of 
                 # $firstVar-a global variable

Simple expressions with operators are expressions that include one operator and two operands. Any of Perl's binary operators can be used in this type of expression.

For example:

10 + $firstVar;  # Adds ten to $firstVar 
  
$firstVar . "AAA"; # Concatenates $firstVar and "AAA" 
  
"ABC" x 5; # Repeats "ABC" five times

A complex expression can use any number of literals, variables, operators, and functions in any sequence.

For example:

(10 + 2) + 20 / (5 ** 2);

20 - (($numPages - 1) * 2);

(($numPages++ / numChapters) * (1.5 / log(10)) + 6);



dave@cs.cf.ac.uk