UNLESS statement in Perl - How to use

In this tutorial of our Perl Course, we will learn what it is, what it is for, and how to use the unless conditional test in Perl.

unless in Perl

We saw in the IF conditional test statement tutorial that it executes a particular block of code if an expression or condition is true.

There is a selection structure contrary to IF, which is UNLESS, ie it will execute certain commands if the conditional test is FALSE.

That is, we use it like this:
unless (condition){
   # code in case
   # condition be false
}
That is, look at the logic of this structure: unless [test] is true, do ...
Now let's look at some examples.

How to use unless in Perl

Create a Perl script that prompts the user to join a party and only allows entry if they are 21 or older.
The script code is:
#!/usr/bin/perl

print "Age: ";
chomp($age=<STDIN>);

unless($age>=21){
        print "You must be 21 at least\n";
}
Now the logic should work well in your head, so you can understand the concept and be a good programmer. What this code means is:

  • print this message, unless the condition is true


That is, if the condition is false, it is to run the unless code.

It is to run, unless it is true, that is, if true does not run, only runs if it is false, unlike IF that only runs if true.
A little confusing at first, but take it easy.

unless and else einm Perl

And, as with our beloved IF and ELSE, there is also UNLESS and ELSE.

Well, if the test is true falls in IF and if false falls in ELSE, in the case of unless it is the opposite: if the test is false, falls in UNLESS and if true, falls in ELSE.

Let's program the previous script more complete:
#!/usr/bin/perl

print "Age: ";
chomp($age=<STDIN>);

unless($age>=18){
        print "You must be 21 ate least\n";
}else{
        print "Just enter!\n";
}
Ready, now if you are 21 or older, warns that you can enter.
If you are under 21, you warn that you cannot enter.

How to use unless

Let's now take the example of the script that asks the password for the user, if it is different from 'rush2112', warns that the hacker will be arrested. If true, warn that you are entering the system.

Here's how the code looks:
#!/usr/bin/perl

print "Type the password: ";
chomp($password=<STDIN>);

unless($password eq 'rush2112'){
        print "Wrong password! We got you, hacker...\n";
}else{
        print "Correct password, entering the system...\n";
}
Note that we use the string equality comparison operator, eq.
If it is false, ie if the password is different from 'rush2112', we say that we caught the hacker.

We could do the opposite, using the inequality (not equal) operator, see:
#!/usr/bin/perl

print "Type the password: ";
chomp($password=<STDIN>);

unless($password ne 'rush2112'){
        print "Correct password, entering the system...\n";
}else{
        print "Wrong password! We got you, hacker...\n";
}
That is, if it is not unequal, it is because the password equals is correct:
It is the opposite of the previous one, and UNLESS is always the opposite of IF.

See that there is no right or wrong, better or worse, code is like fingerprint, each has its own style, see what makes more sense to you.

I particularly still prefer to use IF.

You can even use UNLESS with ELSIF, see:
https://beginnersbook.com/2017/02/unless-elsif-else-statement-in-perl/

No comments:

Post a Comment