Perl Best Practices [Electronic resources]

Damian Conway

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

4.3. Single-Character Strings

Don't write one-character strings in visually ambiguous ways .

Character strings that consist of a single character can present a variety of problems, all of which make code harder to maintain.

A single space in quotes is easily confused with an empty string:

$separator = ' ';

Like an empty string, it should be specified more verbosely:

$separator = q{ };

# Single space

Literal tabs are even worse (and not just in single-character strings):

$separator  = ' ';         # Empty string, single space, or single tab???
$column_gap = '         '; # Spaces? Tabs? Some combination thereof?

Always use the interpolated \t form instead:

$separator = "\t"; $column_gap = "\t\t\t";

Literal single-quote and double-quote characters shouldn't be specified in quotation marks either, for obvious aesthetic reasons: '"', "\", '\'', "'". Use q{"} and q{'} instead.

You should also avoid using quotation marks when specifying a single comma character. The most common use of a comma string is as the first argument to a join:

my $printable_list = '(' . join(',', @list) . ')';

The ',', sequence is unnecessarily hard to decipher, especially when:

my $printable_list = '(' . join(q{,}, @list) . ')';

is just as easy to write, and stands out more clearly as being a literal. See the "Constants" guideline later in this chapter for an even cleaner solution.