Monday, September 13, 2010

if else and unless in perl


Perl provides two forms of simple conditional statement. if-else
Of course Perl also allows if/then/else statements. These are of the following form:

if ($a)
{
print "The string is not empty\n";
}
else
{
print "The string is empty\n";
}
For this, remember that an empty string is considered to be false. It will also give an "empty" result if $a is the string 0.
Using ! and unless
Similarly we can have ! not operator.
In this, it is important to notice that the elsif statement really does have an "e" missing.
Sometimes, it is more readable to use unless instead of if (!...) . The switch-case statement familiar to C programmers are not available in Perl. You can simulate it in other ways. See the manual pages.

elsif 

Note that e is absent in else.
if ($age > $max) {
print "Too old !\n";
} elsif ($age < $min) {
print "Too young !\n";
} else {
print "Just right !\n";
}

No comments:

Post a Comment