Perl::...Pattern(3pm)

πŸ“¦
version 1.0.4 Β· 2 versions
, in debian trixie, ubuntu resolute, ubuntu stonking

✨ NAME ✨

Okay bestie, welcome to Perl::Critic::Policy::Community::SplitQuotedPattern β€” the policy that says: quote the split() pattern argument with regex slashes, mwah πŸ’…

(Also, hi, this is the Perl::...Pattern(3pm) page from the User Contributed Perl Documentation β€” iconic, we love her πŸŽ€)


🌸 DESCRIPTION 🌸

So the first argument to split()? It's a regex pattern, not a string. Periodt. But people keep passing it as a quoted string, which lowkey hides the truth of the situation, and that's exactly how bugs sneak in when your string accidentally has unescaped regex metacharacters in it 😭. And no cap β€” no matter how you quote it, it gets parsed as a pattern anyway (the one exception is the space character special case, which we're getting to, don't worry 🫢).

So the move? Use slashes to quote this argument, so everyone can see with their own eyes that it's a regex pattern. Main character clarity ✨

⚠️ Important caveat, bestie, do NOT skip this: the special case of passing a single space character must be passed as a quoted string, not a pattern. Like, that's the rule.

Also this policy does not warn about passing an empty string, because that's a super common idiom for splitting a string into individual characters, and it doesn't risk containing regex metacharacters. Safe queen πŸ‘‘

By default, this policy also prohibits unquoted patterns like scalar variables, because those don't indicate that the argument is interpreted as a regex pattern and not a string β€” unless it's a string containing a single space character. She's strict like that πŸ’…

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 giving very similar energy to the core policy Perl::Critic::Policy::BuiltinFunctions::ProhibitStringySplit, except it additionally allows empty string split patterns, and disallows unquoted split patterns by default. Glow-up behavior πŸ’–


πŸ¦‹ AFFILIATION πŸ¦‹

This policy is part of Perl::Critic::Community. She runs with her girls 🌈


πŸŽ€ CONFIGURATION πŸŽ€

You can configure this policy to allow passing unquoted patterns (like scalar variables, yes, those ones) by putting an entry in a .perlcritic file like this:

[Community::SplitQuotedPattern]
allow_unquoted_patterns = 1

Do it if you must, bestie, I'm not gonna stop you πŸ™ˆ


πŸ‘‘ AUTHOR πŸ‘‘

Dan Book, "dbook@cpan.org" β€” literal legend, we stan 🫢


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.


⭐ SEE ALSO ⭐

  • Perl::Critic

and that's the whole vibe, bestie ✨ go forth and slash your patterns πŸ¦‹πŸ’–

Signing off from the Perl::...Pattern(3pm) page β€” this one's from perl v5.40.1, dated 2025-03-22, no cap πŸŒΈπŸ’…

πŸ“¦ where this page comes from