Example | Type | Result |

$a+$b | Addition | Sum of $a and $b |

$a-$b | Subtraction | Result of $b subtracted from $a |

$a*$b | Multiplication | Product of $a and $b |

$a/$b | Division | Result of $a divided by $b |

$a%$b | Modulus | Remainder when $a is divided by $b |

$a**$b | Exponentiation | $a to the power of $b |

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.

$a=2; $a=3; print $a+$b #arithmetic operator prints 5 print $a.$b #string operator prints 2 plus the three or 23 print $a*$b #arithmetic operator prints 6 print $a x $b #string operators prints $a $b times or 2 three times. i +e 222

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

$a=3; $b="x"; $c=4; $a*=3; #$a=$a*3; $a now equal to 9; $a/=3; #$a=$a/3; $a (9) divided by three which equals 3; $a+=2; #$a=$a+2; $a is now equal to 5; $a-=2; #$a=$a-2; $a is now equal to 3; $b x=3; #$b=$b x $3 $b is now equal to "xxx"; $b .="33"; #b=$b."33" $b is now equal to "xxx33";

Another assignment operator often used is ||= which sets a variable equal to a value if the value isn't already set.

Type | Numeric | String |

Greater Than | > | gt |

Less Than | < | lt |

Equal to | == | eq |

Not equal | != | ne |

Less than or equal to | <= | le |

Greater than or equal to | >= | ge |

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.

$a=1; print $a++; #prints a as one then adds 1 to it print $a; #now $a is 2 print ++$a; #adds one to $a and then prints its value which is now 3; print $a--; #prints 3 then subtracts one from $a;

Examples | Short Version | Textual Version | Meaning |

$a and $b; $a && b | && | and | returns true if $a and $b are both defined and nonzero |

$a or $b; $a||$b | || | or | returns true if either $a or $b is defined and nonzero |

!$a; not $a | ! | not | returns the opposite of what an expression would otherwise |

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.

You can also use an and to allow something to execute only if the first criteria evaluates to 0;

If you want to find ALL the information on ALL the operators here's your place

Comment on
Operators: arithmetic and otherwise
Select or Download CodeReplies are listed 'Best First'. | |
---|---|

RE: Operators: arithmetic and otherwise by Anonymous Monk on Apr 24, 2000 at 22:31 UTC | |

- Re^2: Operators: arithmetic and otherwise
by tjshankar (Initiate) on Apr 20, 2005 at 00:55 UTC | |

Re: Operators: arithmetic and otherwise by Anonymous Monk on Jan 12, 2001 at 21:10 UTC | |

RE: Operators: arithmetic and otherwise by Falthor (Initiate) on Jun 02, 2000 at 02:52 UTC | |

Re: Operators: arithmetic and otherwise by Anonymous Monk on Jun 29, 2001 at 01:14 UTC | |

- Re: Re: Operators: arithmetic and otherwise
by Anonymous Monk on Mar 17, 2003 at 17:04 UTC | |

Re: Operators: arithmetic and otherwise by lvanhout (Curate) on Jun 15, 2004 at 03:10 UTC |

Back to
Tutorials