Section 12.8. Other Delimiters


12.8. Other Delimiters

Don't use any delimiters other than /.../ or m{...}.

Although Perl allows you to use any non-whitespace character you like as a regex delimiter, don't. Because leaving some poor maintenance programmer to take care of (valid) code like this:

     last TRY if !$!!~m!/pattern/!;

or this:

     $same=m={===m=}=;

or this:

     harry s truman was the 33rd u.s. president;

is just cruel.

Even with more reasonable delimiter choices:

     last TRY if !$OS_ERROR !~ m!/pattern/!;     $same = m#{# == m#}#;     harry s|ruman was |he 33rd u.s. presiden|;

the boundaries of the regexes don't stand out well.

By sticking with the two recommended delimiters (and other best practices), you make your code more predictable, so it is easier for future readers to identify and understand your regexes:

      last TRY if !$OS_ERROR !~ m{ /pattern/ }xms;     $same = ($str =~ m/{/xms  ==  $str =~ m/}/xms);     harry( $str =~ s{ruman was }{he 33rd u.s. presiden}xms );

Note that the same advice also applies to substitutions and transliterations: stick to s/.../.../xms or s{...}{...}xms, and TR/.../.../ or TR{...}{...}.



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