Документация
HTML CSS PHP PERL другое

Section 2.22.  Automated Layout

 
Previous
Table of Contents
Next

2.22. Automated Layout

Enforce your chosen layout style mechanically.

In the long term, it's best to train yourself and your team to code in a consistent, rational, and readable style such as the one suggested earlier. However, the time and commitment necessary to accomplish that isn't always available. In such cases, a reasonable compromise is to prescribe a standard code-formatting tool that must be applied to all code before it's committed, reviewed, or otherwise displayed in public.

There is now an excellent code formatter available for Perl: perltidy. It's freely available from SourceForge at http://perltidy.sourceforge.net and provides an extensive range of user-configurable options for indenting, block delimiter positioning, column-like alignment, and comment positioning.

Using perltidy, you can convert code like this:

    if($sigil eq '$'){
        if($subsigil eq '?'){
            $sym_table{substr($var_name,2)}=delete $sym_table{locate_orig_var($var)};
            $internal_count++;$has_internal{$var_name}++
        } else {
            ${$var_ref} =
                q{$sym_table{$var_name}}; $external_count++; $has_external{$var_name}++;
    }} elsif ($sigil eq '@'&&$subsigil eq '?') {
        @{$sym_table{$var_name}} = grep
            {defined $_} @{$sym_table{$var_name}};
    } elsif ($sigil eq '%' && $subsigil eq '?') {
    delete $sym_table{$var_name}{$EMPTY_STR}; } else
    {
    ${$var_ref}
    =
    q{$sym_table{$var_name}}
    }

into something readable:


    if ( $sigil eq '$' ) {
        if ( $subsigil eq '?' ) {
            $sym_table{ substr( $var_name, 2 ) }
                = delete $sym_table{ locate_orig_var($var) };
            $internal_count++;
            $has_internal{$var_name}++;
        }
        else {
            ${$var_ref} = q{$sym_table{$var_name}};
            $external_count++;
            $has_external{$var_name}++;
        }
    }
    elsif ( $sigil eq '@' && $subsigil eq '?' ) {
        @{ $sym_table{$var_name} }
            = grep {defined $_} @{ $sym_table{$var_name} };
    }
    elsif ( $sigil eq '%' && $subsigil eq '?' ) {
        delete $sym_table{$var_name}{$EMPTY_STR};
    }
    else {
        ${$var_ref} = q{$sym_table{$var_name}};
    }

Notice how closely the tidied version follows the various formatting guidelines in this chapter. To achieve that result, you need to configure your .perltidyrc file like this:


    -l=78   
# Max line width is 78 cols
-i=4
# Indent level is 4 cols
-ci=4
# Continuation indent is 4 cols
-st
# Output to STDOUT
-se
# Errors to STDERR
-vt=2
# Maximal vertical tightness
-cti=0
# No extra indentation for closing brackets
-pt=1
# Medium parenthesis tightness
-bt=1
# Medium brace tightness
-sbt=1
# Medium square bracket tightness
-bbt=1
# Medium block brace tightness
-nsfs
# No space before semicolons
-nolq
# Don't outdent long quoted strings
-wbb="% + - * / x != == >= <= =~ !~ < > | & >= < = **= += *= &= <<= &&= -= /= |= >>= ||= .= %= ^= x="
# Break before all operators

Mandating that everyone use a common tool to format their code can also be a simple way of sidestepping the endless objections, acrimony, and dogma that always surround any discussion on code layout. If perltidy does all the work for them, then it will cost developers almost no effort to adapt to the new guidelines. They can simply set up an editor macro that will "straighten" their code whenever they need to.

    Previous
    Table of Contents
    Next
    © 2000- NIV