Monday, July 18, 2011

goto keyword perl

goto keyword allows you to jump to a particular LABEL declared in the program.But usuage of goto is not recommended as it will spoil the code login.It is better to avoid it

redo keyword perl

redo keyword:
The redo keyword causes Perl to restart the current Statement block.It does not either intialize or increment or decrement.It is
usually used in case of the statements where the input is from another file or from the keyboard.


Content of the Perl file:

{
print ("what is your name? " );
$name=;
chop($name);
content of the perl file:
if ( ! length($name)) {
print ("Zero length name please try a valid name");
redo;
}
print ("Thank you ".uc($name)."\n");

}

OUTPUT:

G:\perl_programs>perl perl_redo.pl
what is your name? karthik
Thank you KARTHIK

G:\perl_programs>perl perl_redo.pl
what is your name?
Zero length name please try a valid namewhat is your name?
Zero length name please try a valid namewhat is your name? kkkk
Thank you KKKK

G:\perl_programs>
As you may see when you give no name it restarts the block to ask the name again.

next keyword perl

The next keyword lets you skip the rest of the statement block and start the next iteration.
It can be used in cases where you want processing only for selected array elements and want
to ignore the rest.



Content of the perl file is:
@array=(1..10);
foreach $var(@array)
{

next if ( $var == 5 || $var == 6 );
print "The var value is $var"."\n";
}

OUTPUT :

G:\perl_programs>perl perl_next.pl
The var value is 1
The var value is 2
The var value is 3
The var value is 4
The var value is 7
The var value is 8
The var value is 9
The var value is 10

G:\perl_programs>
In this case as you may see the var value 5 and 6 are not printed due to the next statement.

last keyword perl

The last keyword is used if you want to break out of the statement block.
This can be used in cases when you iterate through the variables in a array,when you
found the value you want to break out of the loop.


Example:

@array=(1..10);
foreach $var(@array)
{
last if($var==5);
print "The value of var is $var"."\n";
}

OUTPUT:

G:\perl_programs>perl perl_last.pl
The value of var is 1
The value of var is 2
The value of var is 3
The value of var is 4

G:\perl_programs>
Here as you may see from the program it breaks the loop when the value of the $var reaches 5.

Foreach loop perl

It is a special form of for loop in which is specially used for arrays.
It is used to iterate through each element of the array.


SYNTAX:

foreach LOOP_VAR (ARRAY) {
STATEMENTS
}
In this case for each iteration a element of the ARRAY is stored in LOOP_VAR

Example:

@array=(1..10);
foreach $var(@array)
{
print "The var value is $var"."\n";
}

Output:

G:\perl_programs>perl perl_foreach.pl
The var value is 1
The var value is 2
The var value is 3
The var value is 4
The var value is 5
The var value is 6
The var value is 7
The var value is 8
The var value is 9
The var value is 10

G:\perl_programs>

For Loop perl

For loop is mainly used to loop through the STATEMENTS a specific number of times.
You can use it when you want to execute statement specific number of times.


SYNTAX:

For loops:
for(INITALIZATION;CONDITION;INCREMENT/DECREMENT)
{
STATEMENTS
}

In this case the intialization happens first,and the condition is checked,when the condition is passed it executes the statements,Else it comes out
And after executing the Statement,it increments or decrements and checks the condition.If condition passes it again executes else it
comes out.


Example:

for ( $var = 0; $var < 10; $var ++) { print "The var value is $var"; }

Output:

G:\perl_programs>perl perl_for.pl
The var value is 0
The var value is 1
The var value is 2
The var value is 3
The var value is 4
The var value is 5
The var value is 6
The var value is 7
The var value is 8
The var value is 9

G:\perl_programs>

while loop perl

while loop is used to repeat block of statement

Syntax :
It can be used as do-while loop or simply while loop
1)

do
{
STATEMENTS
}while (CONDITION);
In this case the statements gets executed atleast once.For the next iteration the condition in the until has to be true.

2)

while(CONDITION)
{
STATEMENTS;
}
In this case the statements will be executed only when the condition is true.

Example:

1)
Content of the perl file:

$num=20;
do
{
print "the num is $num";
$num++;
}
while($num < 30 )

OUTPUT:

G:\perl_programs>perl perl_while.pl
the num is 20
the num is 21
the num is 22
the num is 23
the num is 24
the num is 25
the num is 26
the num is 27
the num is 28
the num is 29

G:\perl_programs>
2)
Content of the perl file:

$num=20;
while( $num < 30) { print "the num is $num"; $num++; }

OUTPUT: 

G:\perl_programs>perl perl_while.pl
the num is 20
the num is 21
the num is 22
the num is 23
the num is 24
the num is 25
the num is 26
the num is 27
the num is 28
the num is 29

G:\perl_programs>

The main differece between the two in the case of do loop statement will be executed atleast one irrespective of the condition is true or false.

until loop perl

until loop is used to repeat block of statement till the condition is false.

Syntax :
It can be used as do-until loop or simply until loop
1)

do
{
STATEMENTS
}until (CONDITION);
In this case the statements gets executed atleast once.For the next iteration the condition in the until loop has to be false.

2)

until(CONDITION)
{
STATEMENTS;
}
In this case the statements will be executed only when the condition is true.

Example:

1)
Content of the perl file:

$num=20;
do
{
print "the num is $num"."\n";
$num++;
}
until($num >30 )

OUTPUT:

G:\perl_programs>perl perl_until.pl
the num is 20
the num is 21
the num is 22
the num is 23
the num is 24
the num is 25
the num is 26
the num is 27
the num is 28
the num is 29
the num is 30
G:\perl_programs>
2)
Content of the perl file:

$num=20;
until( $num > 30) { 

 print "the num is $num"."\n";
 $num++; 
}

OUTPUT: 

G:\perl_programs>perl perl_until.pl
the num is 20
the num is 21
the num is 22
the num is 23
the num is 24
the num is 25
the num is 26
the num is 27
the num is 28
the num is 29
the num is 30
G:\perl_programs>

The main differece between the two in the case of do loop statement will be executed atleast one irrespective of the condition is true or false.