Previous Table of Contents Next


Assignment Operators

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:

Table 1-4 Some assignment operators

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 (don’t!).

Exponentiation

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 that’s what 24 equals.

        % perl -e ‘$num = 2; $num **= 4; print $num;’
RESULT: 16

Session 2 remarked that -7 isn’t really a simple number, but an operation (unary minus) applied to the number 7. Here’s proof:

        % perl -e ‘ print -7 ** 2’
RESULT: -49

Huh? Squares should always be positive. What’s going on?

Precedence

The - operator has lower precedence than **, so -7 ** 2 is interpreted as -(7 ** 2), NOT as (-7) ** 2. To square -7, you need parentheses.

That’s why 2 + 3 * 5 yields 17 and not 25. * has a higher precedence than +, so that’s 2 + (3 * 5), not (2 + 3) * 5.

Appendix F contains a list of operator precedences.

Autoincrement and Autodecrement

There’s 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 there’s a letter preceding the z, the increment “carries over.”

$monogram = ‘ez’;
$monogram++;

($monogram is now fa.)

And if there’s 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. DON’T.

Arithmetic operators can’t 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.

The ?: Operator

All the operators you’ve seen use either one or two arguments. ?: takes three. It’s 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 isn’t equal to dodo, and dodos otherwise.

1 ? 2 ? 3 : 4 : 5

evaluates to 3. (Because it’s really the same as 1 ? (2 ? 3 : 4) : 5.)

The ! Operator

Here’s 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