top -|- index -|-
samples -|- reference -|-
alphabetic list
arithmetic -|- assignment -|-
Or(|=) -|- And(&=) -|-
increment/decrement -|- numeric -|-
logical
string ( add/concatenation -|- comparison )
-|- end
These are used to perform mathematical calculations on numbers. However, they are NOT used to combine strings as there are some special string operators for this. They are :
Operator | Function |
---|---|
+ | Addition |
- | Subtraction, Negative Numbers, Unary Negation |
* | Multiplication |
/ | Division |
% | Modulus |
** | Exponent |
To use these, you will place them in your statements like a mathematical expression, with or without the space used only to make it more readable. So, to say add two variables into a third variable, you could write something like this :
code:
$revenue = 20; $sales = 10; $total = $revenue + $sales; print "Sales $sales, plus other revenue $revenue, equals total $total\n";
output:
Sales 10, plus other revenue 20, equals total 30
As can be seen, it is quite similar to other programming languages.
Above we have already seen the use of the equal sign, = assignment operator. Here is a list :
Operator | Function |
---|---|
= | Normal Assignment |
+= | Add and Assign |
-= | Subtract and Assign |
*= | Multiply and Assign |
/= | Divide and Assign |
%= | Modulus and Assign |
**= | Exponent and Assign |
It is often 'tidier' to use BITS in a flag...
code:
#!/usr/bin/perl use strict; use warnings; my $F_HADMEM = 8; my $flag = 1; my $msg = ''; $msg = ($flag & $F_HADMEM) ? "Got HADMEM" : "No HADMEM"; print "Value of flag = $flag ($msg)\n"; $flag |= $F_HADMEM; # ADD the BIT to flag $msg = ($flag & $F_HADMEM) ? "Got HADMEM" : "No HADMEM"; print "Value of flag = $flag ($msg)\n"; $flag &= ~$F_HADMEM; # REMOVE the BIT from the flag $msg = ($flag & $F_HADMEM) ? "Got HADMEM" : "No HADMEM"; print "Value of flag = $flag ($msg)\n"; $flag = 1; my $wrap = 0; while ($flag) { print "$flag "; $flag = $flag << 1; # shift bit(s) to left, 1 position $wrap++; if ($wrap > 7) { print "\n"; $wrap = 0; } } print "\n" if ($wrap);
output:
Value of flag = 1 (No HADMEM) Value of flag = 9 (Got HADMEM) Value of flag = 1 (No HADMEM) 1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288 1048576 2097152 4194304 8388608 16777216 33554432 67108864 134217728 268435456 536870912 1073741824 2147483648
As can be seen, it is quite similar to other programming languages.
While a variable can be increased or decreased by 1 by using :-
$variable = $variable + 1; $value = $value - 1;
the '++' or '--' operators can also be used, respectively.
Operator | Function |
---|---|
++ | Increment (Add 1) |
-- | Decrement (Subtract 1) |
For numeric values, the following can be used in a comparison. Note, this is for numeric values only. Strings have their own comparison operators. :
Operator | Function |
---|---|
== | Equal to |
!= | Not Equal to |
> | Greater than |
< | Less than |
>= | Greater than or Equal to |
<= | Less than or Equal to |
Note it is NOT a '+', but a full stop, '.', that joins strings together, like :
Operator | Function |
---|---|
. | Concatenate Strings |
.= | Concatenate and Assign |
For example :-
code:
$firstname = 'John'; # assignment $lastname = 'Doe'; # assignment print "He is called ".$firstname." ".$lastname."\n"; # output concatenation
output:
He is called John Doe
Or :-
code:
$name = 'John'; # assignment $name .= ' '; # concatenation and assign $name .= 'Doe'; # concatenation and assign print "He is called $name.\n";
output:
He is called John Doe.
Operator | Function |
---|---|
eq | Equal to |
ne | Not Equal to |
gt | Greater than |
lt | Less than |
ge | Greater than or Equal to |
le | Less than or Equal to |
Operator | Function |
---|---|
&& | AND |
|| | OR |
! | NOT |
EOF - perl_ops.htm