NAME
Perl::Critic::Policy::ProhibitRegexForSimpleSubstring - Use index() to look for literal text, and leave split its pattern.
VERSION
version 1.000
Perl::Critic::Policy::ProhibitRegexForSimpleSubstring
A regular expression made of nothing but literal characters is a substring search, and index does that without compiling and running a pattern:
if ( $str =~ m/foo/ ) { ... } # reported
if ( index( $str, 'foo' ) >= 0 ) { ... } # what it means
Except where the regex is not a search at all. The first argument of split is the pattern it splits on, and there is no index that splits. Perl::Critic::Policy::BuiltinFunctions::ProhibitStringySplit requires that argument to be a regex rather than a string, so reporting split m/\n/, $text leaves nothing to write that both policies accept. This policy lets split, and anything else named in allow, take a literal regex as its first argument.
This is a fork of Perl::Critic::Policy::Performance::ProhibitRegexForSimpleSubstring by Dean Hamstead. What counts as a simple substring is unchanged; what is new is the list of calls whose first argument is exempt.
PROHIBITED
if ( $str =~ m/foo/ ) { ... }
if ( $str =~ /bar\.baz/ ) { ... } # an escaped dot is still a literal
print if m/foo/;
split $sep, $str =~ m/foo/; # a match, not split's pattern
grep { m/foo/ } @lines;
ALLOWED
split m/\n/, $text;
split /,/, $line;
my @fields = split( m/\t/, $row );
CORE::split( m/:/, $path );
And, as in the original, any regex that is not only literal text:
$str =~ m/foo/i; # a modifier: /i, /m, /s or /x
$str =~ m/^foo/; # an anchor
$str =~ m/fo+/; # a quantifier
$str =~ m/[ab]c/; # a character class
$str =~ m/(foo)/; # a group, even of literal text
$str =~ m/foo|bar/; # alternation
$str =~ m/$foo/; # interpolation
$str =~ s/foo/bar/; # a substitution
my $rx = qr/foo/; # a compiled regex
CONFIGURATION
allow-
Space separated list of functions and methods whose first argument may be a literal regex. It adds to the built-in list rather than replacing it, so you name only your own:
[ProhibitRegexForSimpleSubstring] allow = grep_lines My::Util::match_allThe built-in list is:
split
What a name matches, which is the same rule as Perl::Critic::Policy::ProhibitLeadingZeros's allow:
- A name with no package,
split -
Any call to a function or method of that name, however it is reached:
split(...),CORE::split(...),$obj->split(...)andSome::Class->split(...). - A name with a package,
My::Util::match_all -
Only a call that names that package:
My::Util::match_all(...), or the class methodMy::Util->match_all(...). Not a barematch_all(...), and not$object->match_all(...), since the policy cannot know what package either one ends up in.
Where in the call the regex may be:
- As the first argument
-
Which is where
splittakes its pattern. A literal regex anywhere else in the arguments is a match against$_whose result is being passed, and that is reported like any other:split m/,/, m/x/is a violation, for the second one. - As the whole of that argument
-
split m/\n/, $textis allowed;split $sep, $str =~ m/x/is not, since the regex there is an operand of=~. Next to any operator but a comma, a fat comma or a low-precedenceor,andorxor, a regex is part of an expression rather than an argument. - Of the nearest call
-
foo( split m/\n/, $text )issplit's argument, notfoo's, andsplitdecides. Parentheses around the regex are looked through,split( ( m/x/ ), $s ); a subscript, a block or an anonymous array or hash is not, and a regex inside one is reported.
CAVEATS
A pattern chosen by a ternary, split $tab ? m/\t/ : m/,/, $line, is two operands of ?: rather than an argument, and both are reported. Write the choice as a qr// first, or say ## no critic (ProhibitRegexForSimpleSubstring).
A name with no package matches any method of that name on any object, since there is no knowing what class an invocant is. Name the package if that is too broad.
DIFFERENCES FROM THE ORIGINAL
What moving from [Performance::ProhibitRegexForSimpleSubstring] changes:
A literal regex as the first argument of
split, or of anything named inallow, is not reported. Nothing else is reported differently.A
## no critic (Performance::ProhibitRegexForSimpleSubstring)does not match this policy's name, so an annotation that is still needed has to be renamed to## no critic (ProhibitRegexForSimpleSubstring).
SEE ALSO
Perl::Critic::Policy::BuiltinFunctions::ProhibitStringySplit, which is why split's pattern has to be a regex in the first place.
METHODS
supported_parameters
allow, the functions and methods whose first argument may be a literal regex, added to the built-in list.
initialize_if_enabled
Folds the built-in names back into whatever allow was configured with, so a user's list adds to the defaults instead of replacing them.
default_severity
SEVERITY_MEDIUM
default_themes
performance
applies_to
PPI::Token::Regexp::Match
violates
Standard Perl::Critic::Policy interface. Returns a violation for a match of nothing but literal text, unless it is the whole of the first argument to a call that allow names.
BUGS
Please report any bugs or feature requests on the bugtracker website https://github.com/teodesian/perl-critic-policy-prohibitregexforsimplesubstring/issues
When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.
AUTHORS
Current Maintainers:
George S. Baugh <teodesian@gmail.com>
Original author, of Perl::Critic::Policy::Performance::ProhibitRegexForSimpleSubstring:
Dean Hamstead <dean@fragfest.com.au>
COPYRIGHT AND LICENSE
Copyright (c) 2026 Dean Hamstead, as Perl::Critic::Policy::Performance::ProhibitRegexForSimpleSubstring in Perl-Critic-Policy-Performance-ProhibitRegexForSimpleSubstring.
Modifications are copyright (c) 2026 Troglodyne LLC.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.