unless-elsif-else statement is used when we need to check multiple conditions. In this statement we have only one “unless” and one “else”, however we can have multiple “elsif”. This is how it looks:
unless(condition_1){
   #These statements would execute if
   #condition_1 is false
   statement(s);
}
elsif(condition_2){
   #These statements would execute if
   #condition_1 & condition_2 are true
   statement(s);
}
elsif(condition_3){
   #These statements would execute if
   #condition_1 is true
   #condition_2 is false
   #condition_3 is true
   statement(s);
}
.
.
.
else{
   #if none of the condition is met
   #then these statements gets executed
   statement(s);
}
Example
#!/usr/local/bin/perl
printf "Enter any number:";
$num = <STDIN>;
unless( $num == 100) {
   printf "Number is not 100\n";
}
elsif( $num==100) {
   printf "Number is 100\n";
}
else {
   printf "Entered number is not 100\n";
}
Output:
Enter any number:101 Number is not 100
Leave a Reply