Previous Page
Next Page

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:


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{...}{...}.

    Previous Page
    Next Page