Subject: | Suggested policy: forbid ^ and $ |
Most perl programmers write things like
if ($str =~ /^keyword$/) { ... }
expecting it to be a literal match against 'keyword'. They think that $
matches the end of the string. But it doesn't; this program accepts
"keyword\n" the same as "keyword".
If you use the /m flag always, as another policy suggests, then ^ is
supposed to match the beginning of a line and $ the end. But /^/m is
also hard to understand and bug-prone.
I suggest that people should say what they mean and use \A for the
beginning of the string, \z for the end, and if really wanted \Z for
end-ignoring-final-newline.
Now there is already a policy
RegularExpressions::RequireLineBoundaryMatching. This proposed policy
does not conflict with that, but it does make it obsolete in a way; if
you do not use the ^ and $ anchors then it doesn't matter whether you
use the /m modifier or not.
For more details, see
<http://article.gmane.org/gmane.comp.lang.perl.perl5.porters/61071>,
quoted below:
Show quoted text
>I am mystified as to the circumstances under which one might actually
>want the behaviour of /$/ (without /m) or /^/m. Certainly they can be
>correctly used, with a bit of care, but as far as I can see they never
>completely match the actual semantics of what constitutes a line start
>or end.
Show quoted text>/^/m is so difficult to understand that its
>own implementors have trouble with it. It was documented incorrectly
>in perlre for years, until I discovered the undocumented /(?!\z)/ bit
>of its behaviour and pointed it out (bug #27053, resolved by a
>documentation change in 5.10).