Perl Syntax Basics -

They have: 62 posts

Joined: Dec 1998

Arithmetic

Perl has quite an array of arithmetic operators. All of these work on variables that contain numbers. Many of the operators that are used in the C language are used in Perl as well.

+ (returns the sum of two numbers)
- (returns the difference of two numbers)
* (returns the product of two numbers)
/ (returns the quotient of two numbers)
+= (add a number to a variable)
-= (subtract a number from a variable)
++ (increment a variable by 1)
-- (decrement a variable by 1)

String Manipulation

Some of the operators on numbers have counterparts for strings as well.

. (returns the concatenated, or combined, string of two smaller strings)
.= (appends a string onto a variable)

Logical Operations

These are evaluated as being either True or False(1 or 0), and can help alter the flow of your program using conditional statements

&& (returns true if the first and second are true)
| | (returns true if the first or the second are true)
! (returns true opposite)

== (returns true if the first is equal to the second)
>
<
>=
<=
!=

String Logical Operators

The same operators are different when a variable does not contain numbers.

eq (returns true if first string is equal to the second)
ne (return true if first string is NOT equal to the second)

Some examples of these are:

$foo = $a+5;
$foo++;
$foo += 10;
$foo = "fu" . "bar";
$foo .= "bar";

if ($foo==5){}
if ($foo>10){}
if ($foo>2 && $foo<=5){}
if ($foo!=8){}

if ($foo eq "bar"){}
if ($foo ne "foobar"){}

Statement Blocks
As you just saw with the if (){}, the {} represents a statement block. Inside this can be 1 or more instructions. They can be used with flow control statements, such as if to allow your program to have logical flow.

Flow Control
You can use the if statement together with logical operators and a statement block to add control to your program. An example is:

if ($foo==5){
print "foo is 5";
}

Any combination of logical operators can be used as long as there is only one end result. This may mean using more than one set of parentheses.

if (($foo>5) && ($foo!=10)){
print "foo is between 5 and 10";
}

------------------
Jeffrey Ellison
[email protected]
http://www.eons.com - Free Online Tools for Webmasters