Now we have covered Perl Data types and we should move to conditions statements, Every computer language has condition statements do as Perl do, like other languages Perl also has its owe syntax, functions and keywords to work on condition statements which help us to make decision in condition evaluated through code. We could work on single or multiple statements like if condition provided is true then execute some statements or if not then other we can also manage to evaluate other condition within evaluated conditions statements. In this post, we will discuss Perl if conditions with which we can work on Perl language.

Below flowchart shows, how if condition statements works.

Perl Boolean context comes out of condition will decide condition is True or False. If result would number 0, the strings ‘0’ and “” , the empty list () , and undef are all false. Expect all other are True.

Perl language has various condition statements. We will try to analyze them one by one.

if Condition

This statement is simple one which will evaluate one condition and execute followed statements if condition is True and if not it will end, nothing further.

Syntax

Syntax for if condition

if(boolean_expression) {
   statement execute if the Condition evaluate True
}

I used same Image which used above, because its best fit for it. Let’s have some code example to understand simple if condition statement.

#!/usr/bin/perl

print "What's your name : ";
chomp ($name=<STDIN>);
print "Length of \$name : ", length $name, "\n";

if ( length $name > 5 ) {

print "It's too long\n"

}

This code will work like below.

# perl if_perl 
What's your name : sang
Length of $name : 4

# perl if_perl 
What's your name : sangeeta
Length of $name : 8
It's too long

So now we could see how Perl if condition statement work, it just try to evaluate one condition if it gets True then it execute followed statements and if evaluation is False then it exit

if else Condition

This Condition having also having execution statements with else evaluation. Condition is only one if it evaluate True then execute these statements if it evaluate False then execute these statements.

Syntax

if else condition syntax

if(boolean_expression) {
   statement execute if the Condition evaluate True
} else {
   statement execute if the Condition evaluate False
}

In above image we could see that either Condition evaluation is True or False, statement execution will happen in any way. Let’s see how we could make such code that work with if else Condition and execute statement on evaluation of their Conditions.

#!/usr/bin/perl

print "What's your name : ";
chomp ($name=<STDIN>);
print "Length of \$name : ", length $name, "\n";

if ( length $name > 5 ) {

print "It's too long\n"

} else {

print "It's Perfect\n"

}

In above code we can see that else statement also include after if condition evaluation. Let’s see code output.

# perl if_perl 
What's your name : sang
Length of $name : 4
It's Perfect

# perl if_perl 
What's your name : sangeeta
Length of $name : 8
It's too long



if elsif Condition

This is perfect example of how we could put multiple condition in one program and wait for desire condition with its desired execution statements. This way we could asked Perl program to test or evaluate multiple condition with their statements which provide huge flexibility to any code. I think most important point in If elsif conditions that whenever condition meets, it will execute statements next to it and will not going to check any other condition provided below, once if condition meets it will ignore other condition in same if.

To understand if elsif condition let’s see below code.

# cat if_perl 
#!/usr/bin/perl

print "What's your name : ";
chomp ($name=<STDIN>);
print "Length of \$name : ", length $name, "\n";

if ( length $name > 5 ) {

print "It's too long\n"

} elsif ( length $name <= 0 ) {

print "you need to mention your name...!\n"

} elsif ( $name =~ /^[+-]?\d+$/ ) {

print "Its is number\n";

} else {

print "It's Perfect\n"

}

This code cover many condition, in same we saw how condition evaluate and statement execute. Now first if condition is Fail then it could work on next elsif condition and so on, if any elsif condition evaluate as TRUE then further elsif condition will get ignored.

Let’s see how above code run.

# perl if_perl 
What's your name : sang
Length of $name : 4
It's Perfect

perl if_perl 
What's your name : sangeeta
Length of $name : 8
It's too long

perl if_perl 
What's your name : 12
Length of $name : 2
Its is number

perl if_perl 
What's your name : 
Length of $name : 0
you need to mention your name...!

So is this only way to evaluate condition in Perl. I hope in development environment there is always be scope of other way around, so yes as far I know two more ways to evaluate conditions.

The ?: Operator

This is again a condition operator which act like if else condition that we saw in this post earlier. Actually this condition operator also start with one condition as if condition does which ends with one Question mark (?), further we have statements which will execute only if condition evaluate True and if not then another statements separate by colon (:) will execute. Let’s have one example code below for same, we are writing same logic again with this Syntax

#!/usr/bin/perl

print "What's your name : ";
chomp ($name=<STDIN>);

length $name < 5 ? print "Its's perfect\n"  :  print "It's tooo long\n" ;

Now we have one scalar variable which chomp (get ride of new line) and evaluate it’s length, if length is small then five , it evaluate True , so it will print (It’s perfect) followed by Question mark (?)something but if not evaluate True , then it print next print statement (It’s tooo long) which followed by colon (:). So with all this i hope you now understand this different kind of if else condition.

This would have below output.

# perl if_perl
What's your name : suk 
Its's perfect

# perl if_perl
What's your name : sukrant
It's tooo long