Perl Tutorials
Using WWW-Mechanize
Perl Basics

Perl Basics


Perl offers the common loop structures for, foreach, and while along with do..while and do..until.

For Loop

A for loop is used to parse a block of code X number of iterations. This example will loop though and execute the DO SOMETHING block each time through incrementing the value of $i. As long as $i is less than 10 the code block will be executed.

for ($i=0;$i < 10; $i++) {

Foreach Loop

The foreach is used to loop through a set of values. The foreach loop is commonly used to loop through the values of an array or hash. This example will set a list of values into @array then loop through and print each value.

@array = ('a', 'b', 'c');
foreach (@array) as $value {
print "$valuen";

While Loop

The while loop will execute a code block as long as a condition is true. In the following as long as $i is less than 20 the code block will execute.

my $i = 0;
while ($i < 20) {
print "$in";

do..while do..until Loops

do..while Loop

The do..while loop is the same as a while loop except the conditional is evaluated after the code block is executed. This is favorable if you want the code block to execute at least once.

do {
} while(COND);

do..until Loop

The do..until loop is the opposite of a do..while loop. The do..until loop executes the code block until the condition is true.

do {
} until(COND);

Perl Conditionals <<  1 2 3 4 5  >> Perl Modules
New Content

Related Information
Using WWW-Mechanize