Perl 104: What if… AKA Conditional statements

This post assumes you have read all the preceding ‘Learning to program in Perl’ posts. If you haven’t, you might want to check out the ‘Learning to Program in Perl’ page where you’ll find a complete list of posts in the series.

Decisions, decisions…

All of our example code so far will always do the same thing everytime they run. So for instance:

print "Hello!\n"

Will always produce:

Hello!

Even if we have been abit more clever and included user input:

print "What is your name? ";
my $name = readline(*STDIN);
chomp($name);
print "Hi $name! Pleased to meet you.\n";

The program will not make any sort of decision based on what the user entered. It will just insert a different name. E.g.

What is your name? Toddy
Hi Toddy! Pleased to meet you.

What is your name? Bob
Hi Bob! Pleased to meet you.

Usually, a computer program will need to take different actions depending on the data it has available. For instance, many programs are password protected: The user is prompted to enter a password and if they enter the correct password access is granted. If not they are kept out.

Makes sense, so how do we do that?

Try running this example:

print "Welcome to the Perl Hacker Database. Please enter the password to login: ";
my $password = readline(*STDIN);
chomp($password);
if($password eq '123456') {
   print "Password verified. Access granted\n";
}

You should be prompted to enter a password. If you enter ‘123456’ (the correct password), then you will get an ‘Access Granted’ message. Anything else, and you will get nothing. Let’s break down the new ideas in this example and see how it works:

The if keyword can be used to conditionally run a block of code (wrapped in curly brackets {}), based on if a statement you pass to if is true or false.
In our above example, we pass if:

$password eq '123456'

The eq keyword used here compares two strings (one to the left of the keyword, and one to the right). If they are they same, then it returns a true value, if not, it returns a false value.

But I want to display a message when the user enters the wrong password too!

OK, one approach might be to add another message after the if statement:

print "Welcome to the Perl Hacker Database. Please enter the password to login: ";
my $password = readline(*STDIN);
chomp($password);
if($password eq '123456') {
   print "Password verified. Access granted\n";
}

print "That password is incorrect. Access denied.\n";

Which works great when the user enters the wrong password:

Welcome to the Perl Hacker Database. Please enter the password to login: mypass
That password is incorrect. Access denied.

But not if they enter the right one:

Welcome to the Perl Hacker Database. Please enter the password to login: 123456
Password verified. Access granted
That password is incorrect. Access denied.

The problem is that the last print statement will always be executed, regardless of if the user entered the right password or not. This is better:

print "Welcome to the Perl Hacker Database. Please enter the password to login: ";
my $password = readline(*STDIN);
chomp($password);
if($password eq '123456') {
    print "Password verified. Access granted\n";
}

if ($password ne '123456') {
    print "That password is incorrect. Access denied.\n";
}

Here we have inserted a second if statement, but the condition uses ne. ne is similar to eq in that it compares two strings, but it returns true if the strings are NOT the same, and false if they are the same.

Our new example displays only the correct message to the user, but we can this a little neater:

print "Welcome to the Perl Hacker Database. Please enter the password to login: ";
my $password = readline(*STDIN);
chomp($password);
if($password eq '123456') {
    print "Password verified. Access granted\n";
} else {
    print "That password is incorrect. Access denied.\n";
}

Functionally, this last example is the same as the one before. The user will be displayed the correct message based on if the entered the correct password. But here we have used else instead of a second if statement. An else can be used directly after an if statement. It is followed by a block of code that will be executed if the if condition returns false.

New ideas from this post

New Idea Description Example
if A conditional statement that will execute a block of code if a given statement evaluates to true
if( 2 > 1) {
   print "The statement was true";
}
eq A positive string comparator. It will return true if two strings are the same
if($favourite_food eq "Beans") {
   print "I like beans";
}
ne A negative string comparator. It will return true if two strings are not the same
if($favourite_food ne "Beans") {
   print "I hate beans";
}
else Follows an if statement. Will execute a block of code when the if statement did not evaluates to true
if($favourite_food eq "Beans") {
   print "I like beans";
} else {
   print "I hate beans";
}

Leave a comment