Perl: While Loop

This Comment will be submitted for moderation and will not be accessible to other users until it has been approved.


Perl while loop is quite easy to understand:

 #!/usr/bin/local/perl
 $a = 'programmingbulls'; 
$count = 0; 
while ($count < 10)
{ 
print '$a \n'; 
$count++; 
} 

The above program would print programmingbulls 10 times.

Another useful example can be when asking for an input from user:

 #!/usr/bin/local/perl
 print "Enter Password ";	
 $a = <STDIN>;
 chop $a;
 while ($a ne 'programmingbulls'){
 print 'wrong input \n';
 $a =  chop $a; 
}

The code in curly block of code is executed till the input does not equal the text "programmingbulls".





Post Comment

  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.