Perl::C...difiers(3)

πŸ“¦
version 1.14 Β· 2 versions
, in fedora 43

πŸ’… Perl::Critic::Policy::Bangs::ProhibitUselessRegexModifiers (from the Perl::C...difiers(3) manual, User Contributed Perl Documentation, bestie)


✨ NAME ✨

Perl::Critic::Policy::Bangs::ProhibitUselessRegexModifiers β€” okay so, putting modifiers on a regex that's made entirely out of a variable you made with qr()? Usually not doing what you think it's doing. Not even a little. πŸ™ˆ


🫢 AFFILIATION 🫢

This Policy is part of the Perl::Critic::Bangs distribution. That's its home, that's its whole vibe. πŸ’œ


πŸ”₯ DESCRIPTION πŸ”₯

So here's the lore. In older versions of perl, the modifiers on regular expressions were incorrectly applied. Like, the modifiers weren't landing where you'd expect at all. This got fixed in 5.10 β€” but no warnings were emitted to tell the user that they were probably not getting the effects they were looking for. Which is giving silent chaos, honestly. 😭

Here's the correct way, the slay way:

my $regex = qr(abc)m;
if ( $string =~ /$regex/ ) {};

And here's not what you want:

my $regex = qr(abc);
if ( $string =~ /$regex/m ) {}; ## this triggers a violation of this policy.

Notice how the m hopped from the qr() over to the match? Yeah. That's the thing. The modifier lives on the qr() itself if you want it to count β€” put it on the match instead and it's basically decorative. πŸ’€

See the thread that starts at http://www.nntp.perl.org/group/perl.perl5.porters/2007/12/msg131709.html for a description of how this problem can bite the users. Fr fr, it bites.

And see: http://rt.perl.org/rt3//Public/Bug/Display.html?id=22354 for a description of the bug and subsequent fix. Receipts. πŸ“œ


πŸŽ€ CONFIGURATION πŸŽ€

This Policy is not configurable except for the standard options. That's it, that's the whole menu. πŸ“


πŸ‘‘ AUTHOR πŸ‘‘

Andrew Moore <amoore@mooresystems.com> β€” icon, legend, wrote the thing. ✨


🌸 ACKNOWLEDGMENTS 🌸

Adapted from policies by Jeffrey Ryan Thalhammer <thaljef at cpan.org>, and thanks to Andy Lester, "<andy at petdance.com>" for pointing out this common problem. Shoutout to the group chat for catching it. 🫢


Copyright (c) 2007-2013 Andy Lester <andy@petdance.com> and Andrew Moore <amoore@mooresystems.com>

This library is free software; you can redistribute it and/or modify it under the terms of the Artistic License 2.0. Free as in go off, bestie. πŸ¦‹


And that's the whole page, mwah πŸ’‹ β€” this was perl v5.42.0, dated 2025-07-25, straight from Perl::C...difiers(3). Now go fix your regex modifiers, main character. 🌈⭐

πŸ“¦ where this page comes from