Thursday, 19 February 2015

PERL TUTORIAL PART 3 – ELECDUDE

PERL TUTORIAL PART 3 – ELECDUDE

In this, lets go through the different control structures available in Perl and its examples.

Click here to goto Tutorial Part 1.

Click here to goto Tutorial Part 2.

CONTROL STRUCTURES IN PERL:

               Perl has different control structures, which are as follows:
·       The if/unless Statement
·       The while/until Statement
·       The for Statement
·       The foreach Statement





if  Statement:
            This construct takes a control expression (evaluated for its truth) and a block. It may optionally have an else followed by a block as well.  It is similar to C language.
Syntax:
if (<expression>) {
true_statement; #executes if expression is true
.....
true_statementn;
} else {
false_statement; #executes if expression is FALSE
.....
false_statementn;
}

Another form of IF is the if...elsif...else statement , which has multiple expressions & statements.
Syntax:
if (<expression1>) {
true_statement1; #executes if expression1 is true.
.....
true_statementn1-n;
} elsif (<expression2>) {
true_statement2; #executes if expression1 is false & expression2 is true.
.....
true_statement2-n;
}else {
false_statement1; #executes is both the above expressions are false
.....
false_statementn;
}

unless Statement:
            This construct takes a control expression (evaluated for its falseness) and a block. It may optionally have an else followed by a block as well.  It is same as if statement executing a false condition.
Syntax:
unless (<expression>) {
false_statement; #executes if expression is FALSE
.....
false_statementn;
} else {
true_statement; #executes if expression is true
.....
true_statementn;
}

Another form of IF is the unless...elsif...else statement , which has multiple expressions & statements.
Syntax:
unlsess (<expression1>) {
false_statement1; #executes if expression1 is false.
.....
flase_statementn1-n;
} elsif (<expression2>) {
true_statement2; #executes if expression1 is ture & expression2 is true.
.....
true_statement2-n;
}else {
false_statement1; #executes is both the above expression1 is true & 2 is false
.....
false_statementn;
}

Examples:
$a=5;
if($a==20) {
      printf "\ta is equal 20.\n";
} elsif ( $a < 20 ){
      printf "\ta is less than 20.\n";
}
else {
      printf "\ta is not less than nor equal to 20.\n";
}

unless($a==20) {
      printf "\ta is equal 20.\n";
} elsif ( $a < 20 ){
      printf "\ta is not less than 20.\n";
} else {
      printf "\ta is less than 20.\n";
  }

Loop statements:
            Perl can iterate using the while statement The FOR, FOREACH and WHILE are also known as loop statements, as these repeatedly execute a block of statements for a specific condition.
 WHILE statement executes the statement block till the expression returns TRUE.
while (expression) {
statement_1;
statement_2;
statement_n;     
  }
DO.. WHILE is similar to that of WHILE, but the condition is evaluated at the end of execution of block statements. This is similar to C language. The difference between WHILE & DO WHILE is that, in DO WHILE loop the block of statements is executed once regardless the result of expression.
do{
statement_1;
statement_2;
statement_n;     
} while (expression;

 The UNTIL executes the block of statements till the expression returns FALSE. Replacing the while with until yields the desired effect.
until (expression) {
      false_statement_1;
      false_statement_2;
false_statement_n;
  }

Examples:
$a = 10;
### while
while( $a < 20 ){
   printf "Value of a: $a\n";
   $a = $a + 1;
}
### do..while
$a = 13;
do{
   printf "Value of a: $a\n";
   $a = $a + 1;
}while( $a < 13 );
printf "Now Value of a: $a\n";
### until
$a = 5;
until( $a > 10 ){
   printf "Value of a: $a\n";
   $a = $a + 1;
}

for Statement is another Perl iteration construct, which looks like C or Java's for statement.
Syntax is:
for ( initialise; expression; update ) {
statement1;
statement2;
...
statementn;
}
Example:
for( $a = 10; $a < 20; $a=$a+1 ){
      print "value of a: $a\n";
}

            The foreach loop iterates over a normal list value and sets the variable VAR to be each element of the list in turn. If the variable is preceded with the keyword my, then it visible only within the loop. Otherwise, the variable is implicitly local to the loop and regains its former value upon exiting the loop. The foreach keyword is actually a synonym for the for keyword, so you can use either. If VAR is omitted, $_ is set to each value.

Syntax:
foreach VAR (VAR) {
   statement1;
   ...
   statementn;
}
##
foreach $local (@array1) {
   $local has the elements of @array1 each for every iteration
}
##
foreach (@array1) {
   $_ has the elements of @array1 each for every iteration
}
##
for $local (@array1) {
   $local has the elements of @array1 each for every iteration
}

Example:
@list = (2, 20, 30, 40, 50);
foreach $a (@list){ #$a is local to the loop only
      print "value is: $a\n";
}

my $b=123;
for $b (@list){
#$b is local to the loop with @list contents only, & the former global value of $b not accessible within the loop.
      print "value is: $b\n";
}
#$b regains the value 123.

@list = (2, 20, 30, 40, 50);
foreach my $a (@list){ #$a is local to the loop only
      print "value is: $a\n";
}
print "\na= $a";

my $b=12345;
print "\nb= $b\n\n";
for $b (@list){ #$b is local to the loop & becomes global on loop exit
      print "value is: $b\n";
}
print "\nb= $b";


We welcome your valuable comments and suggestion.
It helps us to do better in future.
Thank you.


0 comments:

Post a Comment

Search Here...