Приглашаем посетить
Огарев (ogarev.lit-info.ru)

Section 2.11.  Tabs

Previous
Table of Contents
Next

2.11. Tabs

Indent with spaces, not tabs.

Tabs are a bad choice for indenting code, even if you set your editor's tabspacing to four columns. Tabs do not appear the same when printed on different output devices, or pasted into a word-processor document, or even just viewed in someone else's differently tabspaced editor. So don't use tabs alone or (worse still) intermix tabs with spaces:

    sub addarray_internal {
    »   my ($var_name, $need_quotemeta) = @_;

    »   $raw .= $var_name;

    »   my $quotemeta = $need_quotemeta ? q{ map {quotemeta $_} }
    »   »   »   »   » :                   $EMPTY_STR
    »   ··············;

    ····my $perl5pat
    ····»   = qq{(??{join q{|}, $quotemeta \@{$var_name}})};

    »   push @perl5pats, $perl5pat;

    »   return;
    }

The only reliable, repeatable, transportable way to ensure that indentation remains consistent across viewing environments is to indent your code using only spaces. And, in keeping with the previous rule on indentation depth, that means using four space characters per indentation level:


    sub addarray_internal {
    ····my ($var_name, $need_quotemeta) = @_;

    ····$raw .= $var_name;

    ····my $quotemeta = $need_quotemeta ? q{ map {quotemeta $_} }
    ··················:···················$EMPTY_STR
    ··················;

    
····
my $perl5pat
········
= qq{(??{join q{|}, $quotemeta \@{$var_name}})};
····
push @perl5pats, $perl5pat;
····
return; }

Note that this rule doesn't mean you can't use the Tab key to indent your code; only that the result of pressing that key can't actually be a tab. That's usually very easy to ensure under modern editors, most of which can easily be configured to convert tabs to spaces. For example, if you use vim, you can include the following directives in your .vimrc file:


    set tabstop=4      
"An indentation level every four columns"
set expandtab
"Convert all tabs typed into spaces"
set shiftwidth=4
"Indent/outdent by four columns"
set shiftround
"Always indent/outdent to the nearest tabstop"

Or in your .emacs initialization file (using "cperl" mode):


    (defalias 'perl-mode 'cperl-mode)

    
;; 4 space indents in cperl mode
'(cperl-close-paren-offset -4) '(cperl-continued-statement-offset 4) '(cperl-indent-level 4) '(cperl-indent-parens-as-block t) '(cperl-tab-always-indent t)

Ideally, your code should not contain a single instance of the tab character. In your layout, they should have been transformed to spaces; in your literal strings, they should all be specified using \t (see Chapter 4).

    Previous
    Table of Contents
    Next