logo
Free, unlimited AI code reviews that run on commit
git-lrc git-lrc GitHub Install Now We'd appreciate a star git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt

Perl::Critic::Policy::Community::SplitQuotedPattern - Quote the split() pattern argument with regex

Affiliation

       This policy is part of Perl::Critic::Community.

Author

       Dan Book, "dbook@cpan.org"

Configuration

       This policy can be configured to allow passing unquoted patterns (such as scalar variables), by putting
       an entry in a .perlcritic file like this:

         [Community::SplitQuotedPattern]
         allow_unquoted_patterns = 1

Description

       The first argument to the split() function is a regex pattern, not a string.  It is commonly passed as a
       quoted string which does not make this clear, and can lead to bugs when the string unintentionally
       contains unescaped regex metacharacters. Regardless of the method of quoting, it will be parsed as a
       pattern (apart from the space character special case described below). Use slashes to quote this argument
       to make it clear that it is a regex pattern.

       Note that the special case of passing a single space character must be passed as a quoted string, not a
       pattern. Additionally, this policy does not warn about passing an empty string as this is a common idiom
       to split a string into individual characters which does not risk containing regex metacharacters.

       By default, this policy also prohibits unquoted patterns such as scalar variables, since this does not
       indicate that the argument is interpreted as a regex pattern and not a string (unless it is a string
       containing a single space character).

         split 'foo', $string;  # not ok
         split '.', $string;    # not ok
         split $pat, $string;   # not ok
         split /foo/, $string;  # ok
         split /./, $string;    # ok
         split /$pat/, $string; # ok
         split qr/./, $string;  # ok
         split ' ', $string;    # ok (and distinct from split / /)

       This policy is similar to the core policy Perl::Critic::Policy::BuiltinFunctions::ProhibitStringySplit,
       but additionally allows empty string split patterns, and disallows unquoted split patterns by default.

Name

       Perl::Critic::Policy::Community::SplitQuotedPattern - Quote the split() pattern argument with regex
       slashes

See Also

       Perl::Critic

perl v5.40.1                                       2025-03-22              Perl::Critic::...itQuotedPattern(3pm)

See Also