Lesson 5 - Variable Manipulation
Summary:

How can I put variables together and change their contents?
What are the operators that are used to manipulate variables?

Last lesson we learned about scalars. This lesson we'll look at some of
the operators that are used to play with them. Unlike last lesson, where we
mostly dealt with strings, most operators we'll use here are for manipulating
numerical scalars.


Basic Mathematical Operators

The first four operators are basic math: `+`, `-`, `*`, and `/`. If
you've never programmed before, the last two might be unfamiliar. They stand
for multiplication and division, respectively. They can be used with plain
numbers, with scalars, or any combination thereof. Example:

-------------------------------------
$x = 5;
$y = $x + 3;
$z = $x - 2;
$x = $y * $z - 4;
print "\$z is $z." # Should print out "$z is 20"

-------------------------------------

Note that order of precedence is multiplication and division, then
addition and subtraction. Equivalent operators are evaluated left to right.
Brackets can be used just as you'd expect to force an order of evaluation.


String Concatenation

`+` works with numbers. What happens when you want to put two strings
together? To do this (called concatenation), the `.` operator is used. It's
used like this:

-------------------------------------
$a = "This is the first half ";
$b = "and this is the second half.";
$c = $a . $c;
print $c . "\n"; # This is useful for tacking a newline onto the end
# of a string to be printed.

-------------------------------------

If you try to use `.` with numbers, it will stick them together as if
they were strings. This is another of the results of dynamic typing, as
mentioned in the previous lesson. If a number is evaluated in a context where a
string is required, it will evaluate as the string equivalent of the number.
(e.g. 15 becomes '15'). If, however, you accidentally try to use a numerical
operator such as `+` with strings (putting a string into a context where a
number is required), it will evaluate as 0. Examples:

-------------------------------------
$v = 'purple' + 'orange'; # $v is 0;
$w = 'orange' + 15; # $w is 15;
$x = 'purple' . 23; # $x is purple23
$y = 23 . 15 # $y is 2315 (not 38)

-------------------------------------


Numerical Incremental/Decremental Operators

Going back to numbers, there are some special operators that can be
used as shorthand. The simplest are the unary operators, `++` and `--`. These
increment and decrement the variable they are applied to by one. Example:

-------------------------------------
$x = 23;
$x++; # $x is now 24.
--$x; # and now back to 23.

-------------------------------------

The first usage is called postfix (after the variable name), the second
is called prefix (before the variable name). Right now it doesn't matter, but
later on prefix and postfix have significance in the order in which things
happen. For now, it's safest just to use postfix.


More Shorthand Operators

The second set of shorthand operators lets you simplify statements like
`$x = $x + 4`. Instead of that, all you need to write is `$x += 4`. There's
condensed operators like this for all four basic math operators and the string
concatenation operator. Examples:

-------------------------------------
$z = 24;
$z -= 4;
$z /= 10; # equivalent to $z = $z / 10. not $z = 10 / $z.
$z *= 3; # $z should now be equal to 6
$z .= ' green bananas'; # ... now '6 green bananas'

-------------------------------------


More Operator Fun

This lesson is designed to serve as an introduction to the basic perl
operators used in common programming. More information about perl operators
such as modulus, exponentiation, and binary logic operators can be found at
http://language.perl.com/newdocs/pod/perlop.html.

Lesson 6 - Arrays/Lists

Home | Lessons | Get Perl | Resources