We finally come to the use of conditionals in Perl (the famous if and else).
If conditional
The basic if structure is like this:
if (condition) {
say "The condition is true";
}
This code means that, if the expression in the condition is true, the message "The condition is true" will be printed on the screen. If false, that entire block of code will be ignored. An example that can be evaluated is the following:
$age = 25;
if ($age >= 18) {
print "Authorized\n";
}
Where, if the variable $age is greater than or equal to 18, the message “authorized” will be written on the screen.
So, let's create a program for a supermarket that tells whether or not a person can buy alcohol and cigarettes. The person will say his age, and the system will say "purchase authorized" if he is at least 18 years old, and if he is not, the system will say "purchase not authorized". The first part of our system is already in our example, time to see how to capture the user's age.
Capturing user input
To keep things smooth, let's capture user input. This entry is captured by the <STDIN> command. Note the example:
print "What is your name?\n";
$nome = <STDIN>;
print "Your name is " . $nome . "\n";
And this is how you capture user input. However, if you've tested this code, you might have noticed one thing: there are two newlines instead of one. This might not be a problem for someone who just wants to print a word at the end of a line, but for anything else the variable is kind of useless. We can solve it with the chomp function:
print "What is your name?\n";
chomp($name = <STDIN>);
print "Your name is " . $name . "\n";
This removes the newline inserted by hitting enter when entering the value. Based on this, and on the fact that in Perl numbers and strings are read in the same way, we can change our code to use ages dynamically, which, in addition to allowing us to test our code with multiple values without making changes to it, also it is part of the functionalities that are necessary for our program to be complete.
If you've done everything right so far, your code should look like this:
chomp($age = <STDIN>);
if ($age >= 18) {
print "purchase authorized\n";
}
Else
But what if the purchase is not authorized? In this case, we will use another conditional: else. The structure syntax is as follows:
if (condition) {
say "the condition is true";
} else {
say "the condition is false";
}
Our code can be implemented in this case as follows:
chomp($age = <STDIN>);
if ($age >= 18) {
print "purchase authorized\n";
} else {
print "purchase not authorized\n"
}
And that's basically it. So, what do you think? Leave your answer in the comments 😆🥰
Exercises
Here are some exercises for you to fix your knowledge:
- Make a program that receives two values and prints the greater
- Make a program that receives a value and says whether it is positive or negative
- Write a program that receives three grades from a student. If the average between them is greater than or equal to 7, say that it is approved, otherwise say that it is disapproved