- PERL Introduction
- Perl - First Script
- Perl - Syntax
- Perl - Variables
- Perl - Scalars
- Perl - Numbers
- Perl - Operators
- Perl - Arrays
- Perl - If
- Perl - For
- Perl - While
- Perl - Hashes
- File - Management
- Database - Management
- Perl Formats
- Perl Error Handeling
- Perl Coding Standard
- Perl Sockets
- Writing Perl Modules
- Object Oriented Perl
- CGI Programming

## Perl Basics

Arithmetic operators are symbols used to execute general arithmetic procedures including: addition (+), subtraction (-), multiplication (*), and division (/).

Operator | Example | Result | Definition |

+ | 7 + 7 | = 14 | Addition |

- | 7 - 7 | = 0 | Subtraction |

* | 7 * 7 | = 49 | Multiplication |

/ | 7 / 7 | = 1 | Division |

** | 7 ** 7 | = 823543 | Exponents |

% | 7 % 7 | = 0 | Modulus |

With these operators we can take a number and perform some simple math operations.

#!/usr/bin/perl print "content-type: text/html \n\n"; #HTTP Header #PICK A NUMBER $x = 81; $add = $x + 9; $sub = $x - 9; $mul = $x * 10; $div = $x / 9; $exp = $x ** 5; $mod = $x % 79; print "$x plus 9 is $add<br />"; print "$x minus 9 is $sub<br />"; print "$x times 10 is $mul<br />"; print "$x divided by 9 is $div<br />"; print "$x to the 5th is $exp<br />"; print "$x modulus 79 is $mod<br />"; |

Your browser should read:

81 plus 9 is 90 |

Assignment operators perform an arithmetic operation and then assign the value to the existing variable. In this example, we set a variable ($x) equal to 5. Using assignment operators we will replace that value with a new number after performing some type of mathematical operation.

Operator | Definition | Example |

+= | Addition | ($x += 10) |

-= | Subtraction | ($x -= 10) |

*= | Multiplication | ($x *= 10) |

/= | Division | ($x /= 10) |

%= | Modulus | ($x %= 10) |

**= | Exponent | ($x **= 10) |

#!/usr/bin/perl print "content-type: text/html \n\n"; #HTTP HEADER #START WITH A NUMBER $x = 5; print '$x plus 10 is '.($x += 10); print "<br />x is now ".$x; #ADD 10 print '<br />$x minus 3 is '.($x -= 3); print "<br />x is now ".$x; #SUBTRACT 3 print '<br />$x times 10 is '.($x *= 10); print "<br />x is now ".$x. #MULTIPLY BY 10 print '<br />$x divided by 10 is '.($x /= 10); print "<br />x is now ".$x; #DIVIDE BY 10 print '<br />Modulus of $x mod 10 is '.($x %= 10); print "<br />x is now ".$x; #MODULUS print '<br />$x to the tenth power is '.($x **= 10); print "<br />x is now ".$x; #2 to the 10th |

$x plus 10 is 15 |

Each time an operation is performed our variable ($x) is permanently changed to a new value of $x.

Relationship operators compare one variable to another. (5 < 12) They are used to compare equality or inequality of two or more variables, be it a string or numeric data.

Logical operators state *and/or* relationships. Meaning, you can take two variables and test an either or conditional. Logical operators are used later on in conditionals and loops. For now, just be able to recognize them in the upcoming examples.

Operator | Example | Defined | Result |

==,eq | 5 == 5 5 eq 5 | Test: Is 5 equal to 5? | True |

!=,ne | 7 != 2 7 ne 2 | Test: Is 7 not equal to 2? | True |

<,lt | 7 < 4 7 lt 4 | Test: Is 7 less than 4? | False |

>,gt | 7 > 4 7 gt 4 | Test: Is 7 greater than 4? | True |

<=,le | 7 <= 11 7 le 11 | Test: Is 7 less than or equal to 11? | True |

>=,ge | 7 >= 11 7 ge 11 | Test: Is 7 greater than or equal to 11? | False |

Operator | Defined | Example |

&&,and | Associates two variables using AND | if (($x && $y) == 5)... |

||,or | Associates two variables using OR | if (($x || $y) == 5)... |

Please note that you must use each different operator depending of whether or not you are comparing strings or numbers. In the table above, the black operators are for numbers and the red ones are for strings.

Variables can be used with mathematical formulas using PERL Operators discussed in a previous lesson. Also, note that variables are case sensitive. "$myvariable," "$MYvariable," and "$Myvariable" can all be assigned different values due to case sensitivity. Numbers of course can be added, subtracted, or multiplied using operators. Strings as shown in the example below can also be used with operators.

#!/usr/bin/perl print "Content-type: text/html \n\n"; #HTTP HEADER #TWO STRINGS TO BE ADDED $myvariable = "Hello,"; $Myvariable = " World"; #ADD TWO STRINGS TOGETHER $string3 = "$myvariable $Myvariable"; print $string3; |

Hello, World |