| Previous | Table of Contents | Next |
Assigning values to variables is usually done with the = sign, as in $answer = <> above. However, programmers frequently perform some operation just prior to the assignment, such as this string concatenation:
$noun = $noun . s
or this addition:
$num = $num + 2
In these cases, you can often combine the operators by saying
$noun .= s
or
$num += 2
Many operator-assignment combinations can be abbreviated, as shown in Table 1-4:
| Statement | Equivalent to | Meaning |
|---|---|---|
| $noun .= s | $noun = $noun . s | Appends an s to $noun. |
| $string x= 3 | $string = $string x 3 | Appends two $strings to $string. |
| $num += 7 | $num = $num + 7 | Increments $num by 7. |
| $actors -= 2 | $actors = $actors - 2 | Decrements $actors by 2. |
| $bam *= 3.14 | $bam = $bam * 3.14 | Multiplies $bam by 3.14. |
| $score /= 0 | $score = $score / 0 | Divides $score by 0 (dont!). |
Perl, unlike C, has an exponentiation operator: **. The following one-line program prints 512 because 83 is 512.
% perl -e print 8 ** 3;
RESULT: 512
Sure enough, **= is a perfectly valid Perl operator. This one-liner prints 16 because thats what 24 equals.
% perl -e $num = 2; $num **= 4; print $num;
RESULT: 16
Session 2 remarked that -7 isnt really a simple number, but an operation (unary minus) applied to the number 7. Heres proof:
% perl -e print -7 ** 2
RESULT: -49
Huh? Squares should always be positive. Whats going on?
The - operator has lower precedence than **, so -7 ** 2 is interpreted as -(7 ** 2), NOT as (-7) ** 2. To square -7, you need parentheses.
Thats why 2 + 3 * 5 yields 17 and not 25. * has a higher precedence than +, so thats 2 + (3 * 5), not (2 + 3) * 5.
Appendix F contains a list of operator precedences.
Theres an even shorter shorthand for the common tasks of adding or subtracting 1 from a number. Instead of saying $var += 1, you can say $var++, which increments $var by 1, returning the value before the increment. ++$var also increments $var, but returns the value after the increment. The -- operator works in a similar fashion, decrementing its operand.
% perl -e $a = 7; $b = $a++; print $a, $b; _;
RESULT: 8, 7
% perl -e $a = 7; $b = $++a; print $a, $b; _;
RESULT: 8, 8
Curiously, the ++ operator can also be applied to strings.
$grade = A; $grade++;
($grade is now B)
$initials = jaa; $initials++;
($initials is now jab)
What happens when you try to increment past z? If theres a letter preceding the z, the increment carries over.
$monogram = ez; $monogram++;
($monogram is now fa.)
And if theres no preceding letter, the increment wraps around to make one.
$name = z; $name++;
($name is now aa!)
$acronym = NBZZ; $acronym++;
($acronym is now NCAA.)
When Is a String not a String?
The behavior of ++ might suggest using other arithmetic operators with strings. DONT.
Arithmetic operators cant really perceive strings. They see 0 instead. So
hambone == tbone
is TRUE because 0 is equal to 0. (Thus, every string is >= every other string, and no string is < any other string.) Always use the string comparators (eq, ne, lt, le, gt, ge, and cmp, introduced in Chapter 4, Session 6) when appropriate.
All the operators youve seen use either one or two arguments. ?: takes three. Its similar to an if statement.
expression1 ? expression2 : expression3
evaluates to expression2 if expression1 is True, and expression3 otherwise. So
($num == 5) ? 6 : 7
evaluates to 6 if $num is 5, and 7 if $num is anything else.
$a = ($string ne dodo) ? $string : $string . s;
sets $a to $string if $string isnt equal to dodo, and dodos otherwise.
1 ? 2 ? 3 : 4 : 5
evaluates to 3. (Because its really the same as 1 ? (2 ? 3 : 4) : 5.)
Heres an operator that takes only one argument: !, which means not. It can be applied to either numbers or strings.
! ($answer eq hello)
is the same as $answer ne hello
!1
is 0, and
!0
is 1, as is
!hello
You could test whether $num is FALSE with
if (!$num) {
print num is zero (or an empty string, or undefined). \n;
}
| Previous | Table of Contents | Next |