Previous Page
Next Page

2.2. Keywords

Separate your control keywords from the following opening bracket.

Control structures regulate the dynamic behaviour of a program, so the keywords of control structures are amongst the most critical components of a program. That's why it's important that those keywords stand out clearly in the source code.

In Perl, most control structure keywords are immediately followed by an opening parenthesis, which can make it easy to confuse them with subroutine calls. It's important to distinguish the two. To do this, use a single space between a keyword and the following brace or parenthesis:


    for my $result (@results) {
        print_sep( );
        print $result;
    }

    while ($min < $max) {
        my $try = ($max - $min) / 2;
        if ($value[$try] < $target) {
            $max = $try;
        }
        else {
            $min = $try;
        }
    }

Without the intervening space, it's harder to pick out the keyword, and easier to mistake it for the start of a subroutine call:

    for(@results) {
        print_sep( );
        print;
    }

    while($min < $max) {
        my $try = ($max - $min) / 2;
        if($value[$try] < $target) {
            $max = $try;
        }
        else{
            $min = $try;
        }
    }

    Previous Page
    Next Page