Wednesday, 9 April 2014

switch statement



Perl switch Statement

A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each switch case.
A switch case implementation is dependent on Switch module and Switch module has been implemented using Filter::Util::Call and Text::Balanced and requires both these modules to be installed.

Syntax:

The synopsis for a switch statement in Perl programming language is as follows:
use Switch;
 
switch(argument){
   case 1            { print "number 1" }
   case "a"          { print "string a" }
   case [1..10,42]   { print "number in list" }
   case (\@array)    { print "number in list" }
   case /\w+/        { print "pattern" }
   case qr/\w+/      { print "pattern" }
   case (\%hash)     { print "entry in hash" }
   case (\&sub)      { print "arg to subroutine" }
   else              { print "previous case not true" }
}
 
The following rules apply to a switch statement:
·         The switch statement takes a single scalar argument of any type, specified in parentheses.
·         The value is followed by a block which may contain one or more case statement followed by a block of Perl statement(s).
·         A case statement takes a single scalar argument and selects the appropriate type of matching between the case argument and the current switch value.
·         If the match is successful, the mandatory block associated with the case statement is executed.
·         A switch statement can have an optional else case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is matched.
·         If a case block executes an untargeted next, control is immediately transferred to the statement after the case statement (i.e. usually another case), rather than out of the surrounding switch block.
·         Not every case needs to contain a next. If no next appears, the flow of control will not fall through subsequent cases.

Example:
#!/usr/local/bin/perl
 
use Switch;
 
$var = 10;
@array = (10, 20, 30);
%hash = ('key1' => 10, 'key2' => 20);
 
switch($var){
   case 10           { print "number 100\n" }
   case "a"          { print "string a" }
   case [1..10,42]   { print "number in list" }
   case (\@array)    { print "number in list" }
   case (\%hash)     { print "entry in hash" }
   else              { print "previous case not true" }
}
 
When the above code is executed, it produces following result:
number 100
Fall-though is usually a bad idea in a switch statement. However, now consider a fall-through case , we will use next to transfer the control to the next matching case which is a list in this case:
#!/usr/local/bin/perl
 
use Switch;
 
$var = 10;
@array = (10, 20, 30);
%hash = ('key1' => 10, 'key2' => 20);
switch($var){
   case 10           { print "number 100\n"; next; }
   case "a"          { print "string a" }
   case [1..10,42]   { print "number in list" }
   case (\@array)    { print "number in list" }
   case (\%hash)     { print "entry in hash" }
   else              { print "previous case not true" }
}
 
When the above code is executed, it produces following result:
number 100
number in list

unless-elsif-else statement



Perl UNLESS...ELSIF statement

An unless statement can be followed by an optional elsif...else statement, which is very useful to test various conditions using single unless...elsif statement.
When using unless, elsif , else statements there are few points to keep in mind.
·         An unless can have zero or one else's and it must come after any elsif's.
·         An unless can have zero to many elsif's and they must come before the else.
·         Once an elsif succeeds, none of the remaining elsif's or else's will be tested.

Syntax:

The syntax of an unless...elsif...else statement in Perl programming language is:
unless(boolean_expression 1){
   # Executes when the boolean expression 1 is false
}
elsif( boolean_expression 2){
   # Executes when the boolean expression 2 is true
}
elsif( boolean_expression 3){
   # Executes when the boolean expression 3 is true
}
else{
   # Executes when the none of the above condition is met
}

Example:

#!/usr/local/bin/perl
 
$a = 20;
# check the boolean condition using if statement
unless( $a  ==  30 ){
    # if condition is false then print the following
    printf "a has a value which is not 20\n";
}elsif( $a ==  30 ){
    # if condition is true then print the following
    printf "a has a value which is 30\n";
}else{
    # if none of the above conditions is met
    printf "a has a value which is $a\n";
}
 
Here we are using equality operator == which is used to check if two operands are equal or not. If both the operands are same then it returns true otherwise it retruns false. When the above code is executed, it produces following result:
a has a value which is not 20

unless-else statement



Perl UNLESS...ELSE statement

A Perl unless statement can be followed by an optional else statement, which executes when the boolean expression is true.

Syntax:

The syntax of an unless...else statement in Perl programming language is:
unless(boolean_expression){
   # statement(s) will execute if the given condition is false
}else{
   # statement(s) will execute if the given condition is true
}
If the boolean expression evaluates to true then the unless block of code will be executed otherwise else block of code will be executed.

Example:

#!/usr/local/bin/perl
 
$a = 100;
# check the boolean condition using unless statement
unless( $a == 20 ){
    # if condition is false then print the following
    printf "given condition is false\n";
}else{ 
    # if condition is true then print the following
    printf "given condition is true\n";
}
print "value of a is : $a\n";
 
$a = "";
# check the boolean condition using unless statement
unless( $a ){
    # if condition is false then print the following
    printf "a has a false value\n";
}else{
   # if condition is true then print the following
    printf "a has a true value\n";
}
print "value of a is : $a\n";
 
When the above code is executed, it produces following result:
given condition is false
value of a is : 100
a has a false value
value of a is : 

unless statement



Perl unless Statement

A Perl unless statement consists of a boolean expression followed by one or more statements.

Syntax:

The syntax of an unless statement in Perl programming language is:
unless(boolean_expression)
{
   # statement(s) will execute if the given condition is false
}
If the boolean expression evaluates to false then the block of code inside the unless statement will be executed. If boolean expression evaluates to true then the first set of code after the end of the unless statement (after the closing curly brace) will be executed.

Example:

#!/usr/local/bin/perl
 
$a = 20;
# check the boolean condition using unless statement
unless( $a < 20 ){
    # if condition is false then print the following
    printf "a is not less than 20\n";
}
print "value of a is : $a\n";
 
$a = "";
# check the boolean condition using unless statement
unless ( $a ){
    # if condition is false then print the following
    printf "a has a false value\n";
}
print "value of a is : $a\n";
 
First unless statement makes use of less than operator (<), which compares two operands and if first operand is less than the second one then it returns true otherwise it returns false. So when the above code is executed, it produces following result:
a is not less than 20
value of a is : 20
a has a false value
value of a is :