NAME

Perl::Critic::Policy::Performance::ProhibitRegexForSimpleSubstring - Use index() instead of regex for literal substring matching

VERSION

version 0.01

DESCRIPTION

When searching for a literal substring in a string, using a regular expression is avoidable overhead. The index() function is significantly faster because it avoids regex compilation and interpretation.

# Bad: regex for simple substring
if ( $str =~ m/foo/ )     { ... }
if ( $str =~ /bar\.baz/ ) { ... }  # escaped dot is still a literal

# Good: use index() instead
if ( index( $str, 'foo' ) != -1 )     { ... }
if ( index( $str, 'bar.baz' ) != -1 ) { ... }

This policy flags regex matches (m// and bare //) that contain only literal characters and have no modifiers. It does not flag:

  • Regexes with modifiers (/i, /m, /s, /x)

  • Regexes with any non-literal tokens (character classes, quantifiers, anchors, groups, interpolation, alternation, etc.)

  • Regexes containing groups ((...), (?:...)), even if the group body is purely literal

  • Substitutions (s///) and compiled regexes (qr//)

AFFILIATION

This policy is part of the Perl-Critic-Policy-Performance-ProhibitRegexForSimpleSubstring distribution.

CONFIGURATION

This policy has no additional configuration options beyond the standard ones.

METHODS

supported_parameters

Returns an empty list. This policy has no configurable parameters.

AUTHOR

Dean Hamstead <dean@fragfest.com.au>

COPYRIGHT AND LICENSE

This software is Copyright (c) 2026 by Dean Hamstead.

This is free software, licensed under:

The MIT (X11) License