GIVEN and WHEN statements in Perl

In this tutorial from our Perl tutorial, we will learn how to use the given conditional test statemtns.

Statement GIVEN in Perl

If you have studied other programming languages, such as C, C ++, or Java, for example, you should be aware of the switch command, which works as a kind of IF and ELSE.

See our tutorial on IF, ELSIF and ELSE in Perl.

Study again the last example, on nested conditional tests statements, where we put several IF and ELSE inside other IF and ELSEs.

Basically, we have a value or expression, where we want to take its result and compare it against a series of possibilities and run a certain code specific to that result.

The given when syntax is as follows:
given (expression){
        when(value1) { #code in case
                       #expression = value1 
                     }
        when(value2) { #code in case
                       #expression = value2 
                     }
        when(value3) { #code in case
                       #expression = value3 
                     }
        default { #code in case
                  #expression be
                  #other value
                }
} 

How works GIVEN and WHEN in Perl

Let's give given some expression, such as a conditional test that results in some value, boolean or not, the information is expression.

Note that each when has a value in it, within the parentheses of each, Perl exits testing expression with all these values within when.

When it finds a value that matches expression, that is, they execute the code of that when.

If it doesn't find any when with a value that matches the given value, it executes the default code.

Usage examples of GIVEN and WHEN in Perl

The following code prompts the user for a number from 1 to 7 and prints the name of the day of the week in full on the screen. If you enter it wrong, it defaults to the problem.
#!/usr/bin/perl

use experimental qw( switch );

print "Week day: ";
chomp($day=<STDIN>);

given ($day){
        when(1) {print "Sunday\n";}
        when(2) {print "Monday\n";}
        when(3) {print "Tuesday\n";}
        when(4) {print "Wednesday\n";}
        when(5) {print "Thursday\n";}
        when(6) {print "Friday\n";}
        when(7) {print "Saturday\n";}
        default {print "Invalid day\n";}
}       

Note a few things.
First, this is possible to program with IF and ELSE, but with them it gets longer and messier.

Second, the GIVEN command is still experimental, in Perl, so for the time being, avoid using it whenever you can.

Let's create now a mini translator, where the user will type the color in English and we will display the translation, in Portuguese:
#!/usr/bin/perl

use experimental qw( switch );

print "Color: ";
chomp($color=<STDIN>);

given ($color){
        when("red") {print "Vermelho\n";}
        when("blue") {print "Azul\n";}
        when("yellow") {print "Amarelo\n";}
        when("Green") {print "Verde\n";}   
        default {print "Color not in database\n";}
}       

Awesome this Perl, isn't it?

Study references Perl

https://www.perlmonks.org/?node_id=1078449
https://www.perltutorial.org/perl-given/

No comments:

Post a Comment