If...then...else...elsif
Previous  Top  Next

If we want to extend that previous example a little bit by defining multiple conditions with elsif, we can use them here:

$number = 15;
if ($number < 10) { print "10" }
elsif ($number < 20) { print "20" }
else { print "Thats ok" }

20 

Now that was easy - and we already learned about the if-then-else statement. There is really not much to add here, except that we can make it a little shorter and easier too if it's not too complex:

$pwd = "dontlikeperl";
print "PWD WRONG YOU LOOSER" if $pwd ne "WeLovePerl!";

Makes sense, right ? But to make it complete, let's print another message if the password is correct !

$pwd eq "Superstar!" ? $output="YOU GOT IT MAN" : $output="PWD WRONG YOU LOOSER";
print $output;

Looks complicated ? Not at all. The evaluation is marked blue and if it is true, the code after the question marked will be executed. If not, the code ofter the colon will be executed. So if your if block only needs to do one statement, use "? :"