There are lists of all the available operators, and what they each do, on pages 76-94 of the Camel. Precedence and associativity are also covered there.
Arithmetic operators can be used to perform arithmetic operations on variables or constants. The commonly used ones are:
Table 5-1. Arithmetic operators
Operator | Example | Description |
---|---|---|
+ | $a + $b | Addition |
- | $a - $b | Subtraction |
* | $a * $b | Multiplication |
/ | $a / $b | Division |
% | $a % $b | Modulus (remainder when $a is divided by $b, eg 11 % 3 = 2) |
** | $a ** $b | Exponentiation ($a to the power of $b) |
Just like in C, there are some short cut arithmetic operators:
$a += 1; # same as $a = $a + 1 $a -= 3; # same as $a = $a - 3 $a *= 42; # same as $a = $a * 42 |
(In fact, you can extrapolate the above with just about any operator - see page 17 of the Camel for more about this)
You can also use $a++ and $a---- if you're familiar with such things. ++$a and ----$a are also valid, but they do some slighty different things and you won't need them today (but you can read about them on pages 17 to 18 of the Camel if you are sufficiently interested).
Just as we can add and multiply numbers, we can also do similar things with strings:
Table 5-2. String operators
Operator | Example | Description |
---|---|---|
. | $a . $b | Concatenation (puts $a and $b together as one string) |
x | $a x $b | Repeat (repeat $a $b times --- eg "foo" x 3 gives us "foofoofoo" |
There's more about the concatenation operator an the top of page 16 of the Camel.
Calculate the cost of 18 widgets at $37.00 each and print the answer (Answer: exercises/answers/widgets.pl)
Print out a line of dashes without using more than one dash in your code (except for the -w). (Answer: exercises/answers/dashes.pl)
Use exercises/operate.pl to practice using arithmetic and string operators.
We can use file test operators to test various attributes of files and directories:
Table 5-3. File test operators
Operator | Example | Description |
---|---|---|
-e | -e $a | Exists - does the file exist? |
-r | -r $a | Readable - is the file readable? |
-w | -w $a | Writable - is the file writable? |
-d | -d $a | Directory - is it a directory? |
-f | -f $a | File - is it a normal file? |
-T | -T $a | Text - is the file a text file? |
There are examples of these in use on pages 19-20 of the Camel. There is a complete list of the file operators in the Camel on page 85. There are lots!
You'll encounter all kinds of other operators in your Perl career, and they're all described in the Camel from page 76 onwards. We'll cover them as they become necessary to us -- you've already seen operators such as the assignment operator (=), the => operator which behaves a bit like the comma operator, and so on.
While we're here, let's just mention what "unary" and "binary" operators are.
A unary operator is one that only needs something on one side of it, like the file operators or the autoincrement (++) operator.
A binary operator is one that needs something on either side of it, such as the addition operator.
A trinary operator also exists, but we don't deal with it in this course. C programmers will probably already know about it, and can use it if they want.