Perl Best Practices [Electronic resources]

Damian Conway

نسخه متنی -صفحه : 317/ 27
نمايش فراداده

2.12. Blocks

Never place two statements on the same line .

If two or more statements share one line, each of them becomes harder to comprehend:

RECORD:
while (my $record = <$inventory_file>) {
chomp $record; next RECORD if $record eq $EMPTY_STR;
my @fields = split $FIELD_SEPARATOR, $record; update_sales(\@fields);$count++;
}

You're already saving vertical space by using K&R bracketing; use that space to improve the code's readability, by giving each statement its own line:

RECORD: while (my $record = <$inventory_file>) { chomp $record; next RECORD if $record eq $EMPTY_STR; my @fields = split $FIELD_SEPARATOR, $record; update_sales(\@fields); $count++; }

Note that this guideline applies even to map and grep blocks that contain more than one statement. You should write:

my @clean_words = map { my $word = $_; $word =~ s/$EXPLETIVE/[DELETED]/gxms; $word; } @raw_words;

not:

my @clean_words
= map { my $word = $_; $word =~ s/$EXPLETIVE/[DELETED]/gxms; $word } @raw_words;