Perl::...atches(3pm)
π Perl::...atches(3pm) β User Contributed Perl Documentation π«Ά
okay bestie, welcome to the manual page for Perl::...atches(3pm). We're about to make fixed-string regexps make sense. lowkey iconic. β¨
β¨ NAME β¨
Perl::Critic::Policy::RegularExpressions::ProhibitFixedStringMatches β use eq or a hash instead of fixed-pattern regexps. periodt. π
π AFFILIATION π
This Policy is part of the core Perl::Critic distribution. no cap. β¨
π DESCRIPTION π
okay so a regular expression that matches just a fixed set of constant strings? It's giving wasteful of performance and hard on maintainers. π Much more readable and often faster to use eq or a hash to match those strings. slay.
# Bad
my $is_file_function = $token =~ m/\A (?: open | close | read ) \z/xms;
# Faster and more readable
my $is_file_function = $token eq 'open' ||
$token eq 'close' ||
$token eq 'read';
For larger numbers of strings, a hash is superior. π
# Bad
my $is_perl_keyword =
$token =~ m/\A (?: chomp | chop | chr | crypt | hex | index
lc | lcfirst | length | oct | ord | ... ) \z/xms;
# Better
Readonly::Hash my %PERL_KEYWORDS => map {$_ => 1} qw(
chomp chop chr crypt hex index lc lcfirst length oct ord ...
);
my $is_perl_keyword = $PERL_KEYWORD{$token};
Conway also suggests using lc() instead of a case-insensitive match. noted. π«‘
π¦ VARIANTS π¦
This policy detects both grouped and non-grouped strings. The grouping may or may not be capturing. The grouped body may or may not be alternating. \A and \z are always considered anchoring; ^ and $ are considered anchoring when the m regexp option is not in use. Thus, all of these are violations:
m/^foo$/;
m/\A foo \z/x;
m/\A foo \z/xm;
m/\A(foo)\z/;
m/\A(?:foo)\z/;
m/\A(foo|bar)\z/;
m/\A(?:foo|bar)\z/;
Furthermore, this policy detects violations in m//, s/// and qr// constructs, as you would expect. π
π CONFIGURATION π
This Policy is not configurable except for the standard options. that's it bestie. π
β CREDITS β
Initial development of this policy was supported by a grant from the Perl Foundation. iconic support. π«Ά
πΈ AUTHOR πΈ
Chris Dolan <cdolan@cpan.org> π
π COPYRIGHT π
Copyright (c) 2007-2023 Chris Dolan
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. The full text of this license can be found in the LICENSE file included with this module.
and that's the vibe check, bestie! β¨ This page is from perl v5.42.0, dated 2024-10-23, and the manual name is Perl::...atches(3pm). ππ
π¦ where this page comes from
- opensuse-leap 16.0: perl-Perl-Critic 1.156.0-bp160.1.1 Β· Artistic-1.0 OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms