< BACKCONTINUE >

5.2 Code Layout

Once you start using loops and conditional statements, you need to think seriously about formatting. You have many options when formatting Perl code on the page. Compare these variant ways of formatting an if statement inside a while loop:

Format A
while ( $alive ) {
    if ( $needs_nutrients ) {
        print "Cell needs nutrients\n";
    }
}
Format B
while ( $alive )
{
    if ( $needs_nutrients )
    {
        print "Cell needs nutrients\n";
    }
}
Format C
      while ( $alive )
        {
           if ( $needs_nutrients )
{
    print "Cell needs nutrients\n";
}
}
Format D
while($alive){if($needs_nutrients){print "Cell needs nutrients\n";}}

These code fragments are equivalent as far as the Perl interpreter is concerned. That's because Perl doesn't rely on how the statements are laid out on the lines; Perl cares only about the correct order of the syntactical elements. Some elements need some whitespace (such as spaces, tabs, or newlines) between them to make them distinct, but in general, Perl doesn't restrict how you use whitespace to lay out your code.

Formats A and B are common ways to lay out code. They both make the program structure clear to the human reading it. Notice how the statements that have a block associated with them—the while and if statements—line up the curly braces and indent the statements within the blocks. These layouts make clear the extent of the block associated with the statements. (This can be critical for long, complicated blocks.) The statements inside the blocks are indented, for which you normally use the Tab key or groups of four or eight spaces. (Many text editors allow you to insert spaces when you hit the Tab key, or you can instruct them to set the tab stops at four, eight, or whatever number of spaces.) The overall structure of the program becomes clearer this way; you can easily see which statements are grouped in a block and associated with a given loop or conditional. Personally, I prefer the layout in Format A, although I'm also perfectly happy with Format B.

Format C is an example of badly formatted code. The flow control of the code isn't clear; for instance, it's hard to see if the print statement is in the block of the while statement.

Format D demonstrates how hard it is to read code with essentially no formatting, even a simple fragment like this.

The Perl style guide, available from the main Perl manual page or from the command line by typing:

perldoc perlstyle

has some recommendations and some suggestions for ways to write readable code. However, they are not rules, and you may use your own judgment as to the formatting practices that work best for you.

< BACKCONTINUE >

Index terms contained in this section

blocks
      code layout, making explicit in
code
      formatting
flow control
      bad code format and
formatting
      code
if statements
      inside a while loop, formatting
interpreter, Perl
      code formats and
layout
      code
Perl
      style guide
perldoc perlstyle
style guide, Perl
while loops
      if statement within, formatting
whitespace
      in Perl programs

© 2002, O'Reilly & Associates, Inc.