Section 4.1. String Delimiters


4.1. String Delimiters

Use interpolating string delimiters only for strings that actually interpolate.

Unexpectedly interpolating a variable in a character string is a common source of errors in Perl programs. So is unexpected non-interpolation. Fortunately, Perl provides two distinct types of strings that make it easy to specify exactly what you want.

If you're creating a literal character string and you definitely intend to interpolate one or more variables into it, use a double-quoted string:

      my $spam_name = "$title $first_name $surname";     my $pay_rate  = "$minimal for maximal work";

If you're creating a literal character string and not intending to interpolate any variables into it, use a single-quoted string:

      my $spam_name = 'Dr Lawrence Mwalle';     my $pay_rate  = '$minimal for maximal work';

If your uninterpolated string includes a literal single quote, use the q{...} form instead:

      my $spam_name = q{Dr Lawrence ('Larry') Mwalle};     my $pay_rate  = q{'$minimal' for maximal work};

Don't use backslashes as quote delimiters; they only make it harder to distinguish the content from the container:

     my $spam_name = 'Dr Lawrence (\'Larry\') Mwalle';     my $pay_rate  = '\'$minimal\' for maximal work';

If your uninterpolated string includes both a literal single quote and an unbalanced brace, use square brackets as delimiters instead:

      my $spam_name = q[Dr Lawrence }Larry{ Mwalle];     my $pay_rate  = q['$minimal' for warrior's work {{:-)];

Reserving interpolating quoters for strings that actually do interpolate something[*] can help you avoid unintentional interpolations, because the presence of a $ or @ in a single-quoted string then becomes a sign that something might be amiss. Likewise, once you become used to seeing double quotes only on interpolated strings, the absence of any variable in a double-quoted string becomes a warning sign. So these rules also help highlight missing intentional interpolations.

[*] Note that "interpolation" includes the expansion of character escapes like "\n" and "\t".

The four distinct rules are fine for isolated literals, but when you're creating a set of related string values, mixing and matching the rules can severely reduce the readability of your code:

     my $title         = 'Perl Best Practices';     my $publisher     = q{O'Reilly};     my $end_of_block  = '}';     my $closing_delim = q['}];     my $citation      = "$title ($publisher)";

For sequences of "parallel" strings, choose the most general delimiters required and use them consistently throughout the set:

      my $title         =  q[Perl Best Practices];     my $publisher     =  q[O'Reilly];     my $end_of_block  =  q[}];     my $closing_delim =  q['}];     my $citation      = qq[$title ($publisher)];

Note that there's a two-column gap between the assignment operator and each q[...] character string. This aligns the string delimiters with those of the lone qq[...] string, which helps its keyword stand out and draws attention to its different semantics.



Perl Best Practices
Perl Best Practices
ISBN: 0596001738
EAN: 2147483647
Year: 2004
Pages: 350
Authors: Damian Conway

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net