Your skill will accomplish
what the force of many cannot
Operators: arithmetic and otherwiseby root (Scribe)
|on Nov 17, 1999 at 01:12 UTC||Need Help??|
The Basic Arithmetic Opertators
To prevent confusion strings have their own operators. Perl has its own addition and mulitply operators for strings. These operators are . and x respectively. We'll show you how these work compared to their arithmetic counterparts.
Assignment simply set values on the left side of a = to what is on the right side. This works for both strings and numbers in Perl. You can speed an assignment like $a=$a*3; by using a handy shortcut used in C and C++. You can simplify variable=variable operator expression to variable operator=expression. We'll demonstrate this with some quick examples.
Another assignment operator often used is ||= which sets a variable equal to a value if the value isn't already set.
Another comparison operator is the <=> operator which returns -1 if the second term is greater, 1 if the first term is greater and 0 if the terms are equal. The string equivalent of <=> is cmp.
Autoincrement and Autodecrement operators These operators simply add or subtract one from a given variable. If the operator comes before the variable the value of the variable after the operation is returned. If the operation comes after the variable the value before the operation is returned. For example
Note those operators are useful for controlling execution based on the way short-circuiting occurs. If you want something to happen only if the first condition isn't met you can use an or.
$a or print '$a is notdefined or is equal to zero';
You can also use an and to allow something to execute only if the first criteria evaluates to 0;
$isMonday and print "Today is Monday\n";
If you want to find ALL the information on ALL the operators here's your place