Developer Forums | About Us | Site Map
Search  
HOME > TUTORIALS > SERVER SIDE CODING > PERL TUTORIALS > ROAD TO BETTER PROGRAMMING: CHAPTER 3. LOOPS, CLEAN CODE, AND THE PERL IDIOMS


Sponsors





Useful Lists

Web Host
site hosted by netplex

Online Manuals

Road to better programming: Chapter 3. Loops, clean code, and the Perl idioms
By Teodor Zlatanov - 2004-02-16 Page:  1 2 3 4 5

Perl loops

This series of articles on developerWorks comprises a complete guide to better programming in Perl. In this third installment, Teodor gives a quick introduction to the Perl loop syntax, conditional statements, and writing clean code. While not intended to teach Perl from the ground up, this chapter will be useful for the beginner or intermediate Perl programmer interested in learning how to apply Perl better to everyday work.

Everyday programming is rarely glamorous -- you have to get your loops straight, your branches right, and your code squeaky clean. Fortunately, Perl makes it easy to write good code with simplified loop constructs, postfix if/unless branching, and a whole lot more. This article takes you through the specific ways in which Perl can help your everyday coding. It is not intended to teach you Perl, but to improve your existing Perl knowledge -- beginner to intermediate.

Perl has the standard procedural looping constructs: the while() loop, the do-until() loop, and the for() loop. These are almost second nature to most programmers. In addition, Perl allows easy iteration over a list of values, or over a range of test conditions.

All of the above loops and the if/unless constructs allow an inverted notation (syntactically, not semantically) in the form EXPRESSION loop or EXPRESSION if/unless as of Perl 5.004. A detailed description of Perl syntax can be found in the perldoc perlsyn page. My recommendation is that you avoid this notation unless you document it well. It can be confusing to some people, especially those with a C/C++/Java background, to see print $_ foreach @array on a line by itself.

The while() loop checks its condition before executing. The following example is a common way to look for input:

Listing 1. The simplest while() loop


while (
)
{
 # do something with the current input here
}

The code above will execute 0 times if there is no input, or N times for N pieces of input. There is no way to control the number of iterations other than by using next/last statements inside the code.

The do-until() loop is similar to the while() loop, but its contents will get executed at least once. It is more appropriate for conditions that depend on something to have been done, for example:

Listing 2. A simple do-until loop



my $i;
do
{
 # put values in @list here...
 ...
 $i = grep(/pattern/, @list);
}
until($i > 0);

In the above example, $i did not have a value until the grep was executed, so instead of assigning it a special fake value and then using a while, we can use a do-until loop and follow a more natural flow. Instead of "until," "while" can be used.

Finally, the for() loop takes three arguments as opposed to the one argument of the do-until() and while() loops. This is why the for() loop is the favorite of C programmers everywhere -- it is very easy to write a for() loop that does absolutely everything in one line. There have been instances of programmers writing a for() loop with no body, because all the work is done in the loop arguments themselves. Such condensed code is welcomed at obfuscation contests, but not in production.

The for() has an initialization section (shockingly, usually used for initializing variables), an iteration section (usually a loop counter gets incremented here), and a test section (used to test whether the loop should keep being executed or not). Thus, the equivalent while() and for() loops would be:

Listing 3. Equivalent for() and while() loops


for ($i=0; $i++; $i < 10)
{
 # do something 10 times
}
$i=0;
while ($i < 10)
{
 # do something 10 times
 $i++;
}

In the above example, $i got incremented from 0 to 9 (that's 10 values). The for() loop certainly looks cleaner, and it is. Perl, however, is not happy to rest on C's laurels (since the for() loop can be most directly traced to the C language). Perl defines an improved syntax for the for() loop, and aliases it to "foreach." This means that anywhere you can use "for" as a standalone language construct in Perl, you can use "foreach," and vice versa.

View Road to better programming: Chapter 3. Loops, clean code, and the Perl idioms Discussion

Page:  1 2 3 4 5 Next Page: For and foreach: how to have fun and get away with it

First published by IBM developerWorks


Copyright 2004-2024 GrindingGears.com. All rights reserved.
Article copyright and all rights retained by the author.