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.
+ - * / **(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.
$a = 'hi'; $b = 'ho'; $song = $a . $b;
0 integer 0.0 float "" null string "0" string () empty array or listEverything 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.
< <= == != >= >== and != mean equal and not equal, respectively.
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.
|| && !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]