
Ok, you must be thinking now “OK, but why did this tutorial have a part 2?”, and I answer you: Perl has more conditional structures than just if and else. One such structure is unless.
See for example the unless command. The unless statement is a conditional structure similar to the if statement, but with inverted logic. It is used to test a condition, and if it is false, the code inside the unless block will be executed. For example:
unless (0) {
print "Entered inside the unless\n";
}
Remembering that the number zero is interpreted by many languages, including Perl, as false, and the number one as true. In this case, as the conditional has a false value, the code inside the block will be executed.
Let's do a simple example: transform the example from the previous tutorial into an example of using unless. Let's start by recalling the given example, which is code for a program to see if a person can buy cigarettes or alcohol based on their age:
print "Enter your age:\n";
chomp($idade = <STDIN>);
if ($idade >= 18) {
print "Purchase authorized\n";
} else {
print "Purchase not authorized\n";
}
Now let's change the order using the unless conditional:
print "Enter your age:\n";
chomp($idade = <STDIN>);
unless ($idade < 18) {
print "Purchase not authorized\n";
} else {
print "Purchase authorized\n";
}
As you can see, the order of the factors has changed without necessarily changing the functionality of the code. That's basically how unless works, and although it's not a clause you'll often find in code, it's good to know more about this conditional structure.
Wassup did you like it? Tell me more in the comments 😄😊
Addendum
User @gwajnberg from Hive said something very important in the last tutorial which was the use of use strict; and use warnings; in his codes. I've omitted these elements throughout the examples, but it's really important that they be taken into account.
Use strict in your code, in short, will force you to write code using a more specific coding style, and will throw an error if you don't. This basically serves to prevent common errors at runtime from happening.
Meanwhile, use warnings basically serves to show possible typos, syntax errors or use of something that doesn't exist. The main difference between both is that the first terminates the program if it has an error and the second does not.
I won't go into much detail about how they both work because these are details that an absolute beginner would hardly understand, but it's important to mention that it's a good practice to always use these two tools in your codes.
To use them in your code, just put them in the first few lines before the code itself starts:
use strict;
use warnings;
print "Enter your age:\n";
chomp($idade = <STDIN>);
unless ($idade < 18) {
print "Purchase not authorized\n";
} else {
print "Purchase authorized\n";
}
If your code still has a hashbang at the beginning, just put it after it:
use strict;
use warnings;
print "Enter your age:\n";
chomp($idade = <STDIN>);
unless ($idade < 18) {
print "Purchase not authorized\n";
} else {
print "Purchase authorized\n";
}
And that’s it.
Exercises
- Write a Perl script that checks whether a number the user passes is even. If it is even, print “This number is even”, while if it is odd, print “This number is odd
- Research the
lengthfunction and write a program that returns the message "Word too short" if a returned word has less than five letters - Take your previous code and add
use strictanduse warnings. Did an error appear? How did you go about solving it?