A switch statement allows testing of a variable equal to multiple values. Each value is called a case, and the variable being tested is checked for each switch case .
The switch case execution is based on the Switch module, which is not installed by default.
Install using CPAN
Open a command window, enter the cpan command, and then enter the install Switch command:
# cpancpan[1]> install Switch // Install cpan[2]> exit // Exit
Ubuntu installation using apt-get
sudo apt-get install libswitch-perl
The syntax format is as follows:
The following are the rules for switch statements:
Scalar parameters of any type can be used in the parentheses of a switch statement.
There can be any number of case statements in a switch. Each case is followed by a value to be compared and a colon.
The scalar after the case statement will be compared with the scalar in the switch statement to determine whether they are equal.
When the variable being tested is equal to the constant in the case, the statements following the case will be executed until the break statement is encountered.
The switch statement can have an optional else statement at the end, which is executed if all cases do not match.
When the case is matched, the case statement block code will be executed, and the switch statement will jump out after execution.
After matching the case, if we need to continue executing the next case statement, we need to add a next statement.
Executing the above program, the output result is:
Number 10
Next let's look at an example of using next :
Executing the above program, the output result is:
Number 10 Numbers in the list