Ads

Saturday, January 17, 2009

Introducing Perl, Part-III

Perl expressions

Now, let’s see how to combine literals and variables using common operators to form expressions.
Numeric expressions and comparisons: Perl supports a number of numeric operators and functions, which take numbers as their arguments and return numbers as their results. The numeric operators are + for addition, - for subtraction, * for multiplication, / for division and ** for exponentiation. log, sin and sqrt are some numeric functions. And for numeric comparison, you can use <, <=, ==, !=, >= and > which have their usual meaning.
A combination of numeric data and operators is called a numeric expression.

String expressions and comparison: Basically there is only one string operators ‘.’, which is used to concatenate two strings. You can use le, lt, eq, ne, ge and gt for string comparison.

Boolean expressions and operators: often an expression’s result is important for whether it indicates truth or falsity. 0, 0.0, “”, “0”, and () mean false and anything other than these mean true. Common Boolean operators are ||, && and ! first two are binary operators ‘OR’ and ‘AND’ respectively. The ‘!’ is the not operator.

The ‘IF’ Statement

As in any other programming language, Perl also supports ‘if’ statements and its variations. The ‘if’ statement allows you to execute some set of statements dependent upon a condition being satisfied. For example,

if ($name eq ‘Euler’) {
print “Your name is Euler.”;
}

There can also be else conditions, e.g;

if ($name eq ‘Maan’) {
print “Your name is Maan”;
} elseif ($name eq ‘Sachin’) {
print “Your name is Sachin”;
} else {
print “You are not Maan nor sachin”;
}

No comments: