NAME
Perl::Critic::Policy::Community::SplitQuotedPattern - Quote the split() pattern argument with regex slashes
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.
AFFILIATION
This policy is part of Perl::Critic::Community.
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
AUTHOR
Dan Book, dbook@cpan.org
COPYRIGHT AND LICENSE
Copyright 2024, Dan Book.
This library is free software; you may redistribute it and/or modify it under the terms of the Artistic License version 2.0.