Skip Menu |
 

Preferred bug tracker

Please visit the preferred bug tracker to report your issue.

This queue is for tickets about the Perl-Critic CPAN distribution.

Report information
The Basics
Id: 38292
Status: open
Priority: 0/
Queue: Perl-Critic

People
Owner: Nobody in particular
Requestors: EDAVIS [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: (no value)
Fixed in: (no value)



Subject: Suggested policy: forbid ^ and $
Download (untitled) / with headers
text/plain 1.5k
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).
Subject: Re: [rt.cpan.org #38292] Suggested policy: forbid ^ and $
Date: Thu, 07 Aug 2008 12:45:21 -0500
To: bug-Perl-Critic [...] rt.cpan.org
From: Elliot Shank <perl [...] galumph.com>
See RegularExpressions::ProhibitFixedStringMatches.
Download (untitled) / with headers
text/plain 290b
Show quoted text
>See RegularExpressions::ProhibitFixedStringMatches.
A good policy, but not quite the issue addressed here. For example to crudely match an HTML heading start tag, /^<h[1-6]>$/ is buggy - it allows a trailing newline. /\A<h[1-6]>\z/ is correct (for the purpose of this example).


This service is sponsored and maintained by Best Practical Solutions and runs on Perl.org infrastructure.

Please report any issues with rt.cpan.org to rt-cpan-admin@bestpractical.com.