More Perl Operators
There are many more operators in Perl besides the more basic ones.
Perl Operators |
|
Exponentation | Exponentational provides away to multiply a number to itself repeatedly |
Remainder | Retrieves the remainder resulting from division of one integer by another |
Unary Negation | Is a character in front of a single value, it is equivalent to multiplying the value by -1 |
Integer Comparison | There are many comparison operators - see Perl Cheat Sheet, becareful when comparing floating-point numbers as rounding up may make numbers different |
String Comparison | There are many comparison operators - see Perl Cheat Sheet |
Logical | Can used to to check for multiply conditions like a if-else statement, Short-circuit evaluation means that in some circumstances if a part of the evaulation forces the outcome of the statement to be true or false regardless if the other part outcome if ( $age == 5 || $age == 10 ) ## the second condition will not be checked of $age equals 5, the outcome will be true anyway |
Bit-Manipulation | You can manipulate the binary digits (or bits) of an integer there are many bit-manipulation operators - see Perl Cheat Sheet |
Assignment | Associates or assigns a value to a variable, you can use the operator more than once in a single statement |
Autoincrement and autodecrement | The autoincrement and autodecrement is a third way to increment/decrement variables by 1, you can also increment/decrement strings. |
String concatenation | There are a number of operators that can effect strings - see Perl cheat sheet |
Repetition | Make multiple copies of a string and joins the copies together. |
Comma | Guarantees that a particular part of an expression is evaluated first |
Conditional | This is know as a tenary operator in other languages and is based on a if-else statement |
Perl Operators Examples |
|
Exponentation | $x = 2 ** 4; # take four copies of two and multiply them (2 * 2 * 2 * 2 = 16) $ x = 2 ** -5; # this is the fraction 1/32 |
Remainder | $x = 25 % 4; # 25 divided by 4 yields 6 with a remainder of 1, so $x = 1 |
Unary Negation | $x = - 5; # $x = -5; $x = - $y ; # $x = $y * -1; |
Integer Comparison | if ( $a == $b ) { .... } |
String Comparison | if ( $stringA eq $stringB ) { .... } if ( $stringA lt $stringB ) { .... } if ( $stringA gt $stringB ) { .... } if ( $stringA le $stringB ) { .... } if ( $stringA ge $stringB ) { .... } if ( $stringA ne $stringB ) { .... } if ( $stringA <cmp> $stringB ) { .... } |
Logical | if ( $age > 5 && $age < 17 ) { .... } if ( ! $x ) { .... } # true if $x is not zero and false if $x is 0; |
Bit-Manipulation | $x = 124 & 99; # 01111100 (124) & 01100011 (99) = 01100000 (96) |
Assignment | $a = 5; # assign the value 5 to the variable $a $a += 5; # really means $a = $a + 5 $a *= 10; # really means $a = $a * 5 $a ^= 95; # really means $a = $a ^ 5 |
Autoincrement and autodecrement | $a++ # post-increment ++$a # pre-increment $a-- # post-decrement --$a # pre-decrement |
String concatenation | $you_are_a = $potatoe . $head; # this concatenation variables $potatoe, $head into one string value |
Repetition | $strong_mint = "X" x 3; # procedues the string "XXX" |
Comma | $var1 += 1, $var2 = $var1; # the list of statements will be processed from left to right |
Conditional | $result = $var == 0 ? 14 : 7; # if $var equals 0 then $result = 14, if $var is not equal to 0 then # $result = 7 |
Precedence
Operators are governed by a set of rules, these are called the 'rules of precedence', the precedence will determine the process statement order , highest being first, You can force the order of precedence by using brackets
Force Precedence | $result = 4 * ( 5 + 3 ); # Normally the mulitplication would be processed first, # but by using brackets we force the addition to be # processed first |
See Perl Cheat Sheet for a complete list of precedence.