|Contents||Perl Arithmatic operators|
PERL - Arithmetic Operators
Arithmetic operators are symbols used to execute general arithmetic procedures including: addition (+), subtraction (-), multiplication (*), and division (/).
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
81 minus 9 is 72
81 times 10 is 810
81 divided by 9 is 9
81 to the 5th is 3486784401
81 modulus 79 is 2
PERL - Assignment Operators
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.
#!/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
x is now 15
$x minus 3 is 12
x is now 12
$x times 10 is 120
$x divided by 10 is 12
x is now 1201
x is now 12
Modulus of $x mod 10 is 2
x is now 2
$x to the tenth power is 1024
x is now 1024
Each time an operation is performed our variable ($x) is permanently changed to a new value of $x.
PERL - Logical & Relational Operators
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.
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.
PERL - Variables + Operators
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;