[ Tutorial Page | Glossary | Course Page | Lectures ]

Civil Engineering 82.562 -- The Perl Programming Language

Expressions

----------

We have spent some time looking at literal data (constants) and variables. Now it is time to see how these may be combined in expressions. This section only mentions the more "common" operators and functions. There are lots; see the manual.

Numeric Expressions

Perl has a pretty full range of numeric operators and functions, and they behave pretty well as you would expect. Numeric operators and functions expect numbers as arguments and return numbers as results. The numeric operators are:
	+ - * / **
(the latter meaning exponentiation, of course). Some built-in numeric functions are:
	cos sin sqrt exp log sqrt
(actually, truth be told, these are really single argument operators rather than functions, but don't worry too much about the difference). And of course, precedence of operations can be over-ridden by use of parenthesis. So, we may have:
	$a = 3.0;
	$b = 4.0;
	$c = sqrt( $a**2 + $b**2 );

Even numeric operators and functions expect numeric arguments, if they are given a string argument they convert to numeric automatically. This saves you a lot of bother.

String Expressions

There is really only one string operator, and that is '.', which is string concatenation. So:
	$a = 'hi';
	$b = 'ho';
	$song = $a . $b;

Boolean Expressions

Often an expression result is important for whether it indicates truth or falsity. There is no special data type that is used for this, just special values that mean false. Here are all the values that indicate false when used in a context that needs to know:
	0	integer
	0.0	float
	""	null string
	"0"	string
	()	empty array or list
Everything else will mean true. There are a number of operators that explicitly produce boolean results (i.e., they are comparisons of one kind or another). These always produce 0 to indicate false and 1 to indicate true.

Numeric Comparisons

For comparing numbers, use one of the following operators:
	<  <=  ==  !=  >=  >
== and != mean equal and not equal, respectively.

String Comparisons

For comparing strings, use one of the following operators:
	lt  le  eq  ne  ge  gt

One must be careful here. If you compare strings using the numeric comparison operators, the strings are first converted to numeric quantities and the numeric quantities compared; the answer may suprise you.

The most important string comparison in Perl is probably the pattern match. The pattern match operators are:

	=~  !~
(for does, does not match respectively). The idea of pattern matching is so powerful and so important, it is left for a later lesson.

Boolean (logical) Operators

Some operators expect boolean operands and produce a boolean result. The most common are:
	||  &&  !
The first two are binary operators: or and and respectively. They are both "short-circuit" operators; the right operand is not evaluated if the answer can be determinded solely from the left operand.

'!' is the not operator -- it reverses (in a boolean sense) the value of its only operand.

Of course, this precedence of all these operators is important and we haven't said a thing about that. The precedences are largely intuitive; either see the manual if you really must know, or use lots of parentheses (you probably should, anyway).

[Quiz | Next Lesson]


-- NMH, Mon Jan 16 15:15:16 EST 1995