Now that we have seen how to utilize variables a little bit, lets experiment with manipulating them. The first thing we will try is some simple arithmetic on numbers. In Perl, there are numerous basic arithmetic operations:
| Symbol | Description |
|---|---|
| + | Addition between two numbers |
| - | Subtraction between two numbers |
| * | Multiplication between two numbers |
| / | Division between two numbers |
| % | The mod (remainder) of two numbers |
| . | The concatenation of two numbers or strings |
| ++ | Unary increment, use before or after the variable. |
| -- | Unary decrement, use before or after the variable. |
| +=, *=, /=, -=, %=, .=, etc. | Shortcuts that give an immediate result. |
Lets see some examples of these in action on some numbers:
#!/usr/bin/perl -w
$number1 = 5;
$number2 = 7;
print "number1: ", $number1, " number2: ", $number2, "\n\n";
$number3 = $number1 + $number2;
print $number1, " + ", $number2, " = ", $number3, "\n\n";
$number3 = $number1 - $number2;
print $number1, " - ", $number2, " = ", $number3, "\n\n";
$number3 = $number1 * $number2;
print $number1, " * ", $number2, " = ", $number3, "\n\n";
$number3 = $number1 / $number2;
print $number1, " / ", $number2, " = ", $number3, "\n\n";
print "\tFormated : ", $number1, " / ", $number2, " = ",
sprintf("%.2f", ($number1 / $number2)), "\n\n";
$number3 = $number1 % $number2;
print $number1, " % ", $number2, " = ", $number3, "\n\n";
$number3 = $number1 . $number2;
print $number1, " . ", $number2, " = ", $number3, "\n\n";
print $number1, "\n\n";
$number1++;
print $number1, "\n\n";
$number1--;
print $number1, "\n\n";
The output of this little script looks like so:
number1: 5 number2: 7
5 + 7 = 12
5 - 7 = -2
5 * 7 = 35
5 / 7 = 0.714285714285714
Formated : 5 / 7 = 0.71
5 % 7 = 5
5 . 7 = 57
5
6
5
Perl will give you as much precission as possible when it comes to
numbers, so it becomes necessary to format them. The C style
sprintf() function can be used to format
your numbers.
Like most other languages, Perl also has options to perform 'bitwise'
operations:
| Symbol | Description |
|---|---|
| & | Bitwise AND between two numbers |
| | | Bitwise OR between two numbers |
| ^ | Bitwise XOR between two numbers |
| >> | Right Shift the bits (divide by 2) |
| << | Left Shift the bits (multiply by 2) |
Lets see some examples of these in action on some numbers:
#!/usr/bin/perl -w
$number1 = 5;
$number2 = 7;
print "number1: ", $number1, " number2: ", $number2, "\n\n";
# Remember that 5 = 0101 & 7 = 0111
$number3 = $number1 & $number2;
print $number1, " & ", $number2, " = ", $number3, "\n\n";
# 0101
# AND 0111
#---------
# 0101 = 5
$number3 = $number1 | $number2;
print $number1, " | ", $number2, " = ", $number3, "\n\n";
# 0101
# OR 0111
#---------
# 0111 = 7
$number3 = $number1 ^ $number2;
print $number1, " ^ ", $number2, " = ", $number3, "\n\n";
# 0101
# XOR 0111
#---------
# 0010 = 2
$number3 = $number1 >> 1;
print $number1, " > 1 ", " = ", $number3, "\n\n";
# 0101
#---------
# 0010 = 2
$number3 = $number1 << 1;
print $number1, " < 1 ", " = ", $number3, "\n\n";
# 0101
#---------
# 01010 = 10
The output of this little script looks like so:
number1: 5 number2: 7
5 & 7 = 5
5 | 7 = 7
5 ^ 7 = 2
5 >> 1 = 2
5 << 1 = 10
An interesting, if not disturbing, little trick about Perl can be accomplished by storing a 'numeric' value in a 'string' format. Even though you as the programmer may have intended to use it as a string, the Perl interpreter will still allow you to perform all arithmetic values on the 'string'. Here is an example:
#!/usr/bin/perl -w
$string1 = "hi";
$string2 = " there";
print "string1: ", $string1, "\tstring2: ", $string2, "\n";
$result = $string1 + $string2;
print $string1, " + ", $string2, " = ", $result, "\n\n";
$string1 = "8";
$string2 = "10";
print "string1: ", $string1, "\tstring2: ", $string2, "\n";
$result = $string1 + $string2;
print $string1, " + ", $string2, " = ", $result, "\n";
The output of this little script looks like so:
string1: hi string2: there
Argument " there" isn't numeric in addition (+) at ./string_num.pl line 9.
Argument "hi" isn't numeric in addition (+) at ./string_num.pl line 9.
hi + there = 0
string1: 8 string2: 10
8 + 10 = 18
This feature can be a blessing if you need to handle numbers as both strings and numeric values, but if you are not expecting it, it may give you some problems. A more useful usage of this feature is coupled with the eval() function. This function allows you to evaluate an arithmetic expression, even though it is in a string format. Here is an example of it's usage:
#!/usr/bin/perl -w
$string1 = "10";
$string2 = "3";
@ops = (" + ", " - ", " * ", " / ");
foreach $op (@ops) {
$result = eval($string1 . $op . $string2);
print $string1, $op, $string2, " = ", $result, "\n";
}
The output of this little script looks like so:
10 + 3 = 13
10 - 3 = 7
10 * 3 = 30
10 / 3 = 3.33333333333333
Not to be outdone by all of the arithmetic operations, there is a wide range of operations that can be applied to regular strings as well. Some of these operators are:
| Symbol | Description |
|---|---|
| . | Concatenation between two numbers or strings |
| x | Repeat 'x' number of times (sort of like a multiply) |
| & | Bitwise AND between two strings |
| | | Bitwise OR between two strings |
| ^ | Bitwise XOR between two strings |
| split | Allows you to split up a string by a 'token'. |
Here are some examples of the string functions in action:
#!/usr/bin/perl -w
$string1 = "hi ";
$string2 = "there";
$string3 = $string1 . $string2;
print $string1, " . ", $string2, " = ", $string3, "\n\n";
@string_array = split / /, $string3;
print $string_array[0], "\t", $string_array[1], "\n\n";
$string3 = $string1 x 4;
print $string1, " x 4", " = ", $string3, "\n\n";
$string3 = $string1 & $string2;
print $string1, " & ", $string2, " = ", $string3, "\n\n";
$string3 = $string1 | $string2;
print $string1, " | ", $string2, " = ", $string3, "\n\n";
$string3 = $string1 ^ $string2;
print $string1, " ^ ", $string2, " = ", $string3, "\n\n";
The output of this little script looks like so:
hi . there = hi there
hi there
hi x 4 = hi hi hi hi
hi & there = `h
hi | there = |iere
hi ^ there = Ere
Besides regular base 10 numbers and strings, it is also possible to represent hexadecimal, octal, and binary numbers in Perl. With these capabilities also comes the ability to use all of the pre-mentioned operations such as addition, subtraction, logical stuff, and the like. Here is a very brief example of how to represent these numbers:
#!/usr/bin/perl -w
$oct = 0377;
# Precede the digits with a 0 (zero)
$hex = 0xffff;
# Precede the digits with a 0x (zero, lowercase x)
$bin = 0b0000_0011;
# Precede the digits with a 0b (zero, lowercase b), you can
# also use a _ (underscore) to break into chunks of digits
print $oct, "\n\n";
print $hex, "\n\n";
print $bin, "\n\n";
The output of this little script looks like so:
255
65535
3
In addition to these operations, it is also necessary to perform comparisons between variables. It is possible to do this on both the strings and numbers. Here is a chart to illustrate the operators:
| Numeric Symbol | String Symbol | Description |
|---|---|---|
| > | gt | True when the first number or string is greater than the second. |
| < | lt | True when the first number or string is less than the second. |
| >= | ge | True when the first number or string is greater than or equal to the second. |
| <= | le | True when the first number or string is less than or equal to the second. |
| == | eq | True when the first number or string is equal to the second. |
| != | ne | True when the first number or string is not equal to the second. |
| <=> | cmp | Works much like the fortran arithmetic if, returns a -1 when the result is negative, 0 when it is 0, and a +1 when it is positive. |
| && | AND | Use to conjoin two statements, only true when both are true. |
| || | OR | Use to conjoin two statements, only true when both, or either are true. |
| ! | NOT | Use to reverse a term. |
We will see examples of these in action in the next section on Control Structures.