Perl::...utTest(3pm)

πŸŽ€ NAME πŸŽ€

okay so bestie, this is the 3pm manual page (User Contributed Perl Documentation) for Perl::Critic::Policy::RegularExpressions::ProhibitCaptureWithoutTest β€” the header shorthand is Perl::...utTest(3pm) β€” and its whole thing is: capture variable used outside conditional. πŸ’…

πŸ’– AFFILIATION πŸ’–

This Policy is part of the core Perl::Critic distribution. slay, it ships with the main crew. ✨

πŸ”₯ DESCRIPTION πŸ”₯

Okay so picture this: a regexp match fails, and your capture variables ($1, $2, ...) are just... unaffected? 😭 They retain whatever old values they may have had, like they didn't get the memo. So it's super important to check the return value of a match before you use those variables. No cap.

Example time:

'12312123' =~ /(2)/;
print $1;    # Prints 2
'123123123' =~ /(X)/;
print $1;    # Prints 2, because $1 has not changed.

See? The second match failed, but $1 is still out here printing 2. iconic but also a trap. πŸͺ€

And because the values of $1 etc. will be unaffected, you cannot determine if a match succeeded by checking to see if the capture variables have values. Absolutely not.

# WRONG
$str =~ /foo(.+)/;
if ( $1 ) {
    print "I found $1 after 'foo'";
}

This policy checks that the previous regexp for which the capture variable is in-scope is either in a conditional or causes an exception or other control transfer (i.e. next, last, redo, return, or sometimes goto) if the match fails. Basically: prove you handled the fail, bestie. πŸ‘‘

A goto is only accepted by this policy if it is a co-routine call (i.e. goto &foo) or a goto LABEL where the label does not fall between the goto and the capture variable in the scope of the goto. A computed goto (i.e. something like goto (qw{foo bar baz})[$i]) is not accepted by this policy because its target can not be statically determined. no vibes-based jumps, sorry. 🚫

This policy does not check whether that conditional is actually testing a regexp result, nor does it check whether a regexp actually has a capture in it. Those checks are too hard. It said what it said. πŸ’€

This policy also does not check arbitrarily complex conditionals guarding regexp results, for pretty much the same reason. Simple things like

m/(foo)/ or die "No foo!";
die "No foo!" unless m/(foo)/;

will be handled, but something like

m/(foo)/ or do {
  ... lots of complicated calculations here ...
  die "No foo!";
};

are beyond its scope. it's giving β€œnot my department.” πŸ™ˆ

🌸 CONFIGURATION 🌸

By default, this policy considers die, croak, and confess to throw exceptions. If you have additional subroutines or methods that may be used in lieu of one of these, you can configure them in your perlcriticrc as follows:

[RegularExpressions::ProhibitCaptureWithoutTest]
exception_source = my_exception_generator

custom exception source? we love to see it. 🫢

πŸ¦‹ BUGS πŸ¦‹

This policy does not recognize named capture variables. Yet. (the β€œyet” is doing so much work, honestly) πŸ‘€

πŸ‘‘ AUTHOR πŸ‘‘

Chris Dolan <cdolan@cpan.org>

Copyright (c) 2006-2017 Chris Dolan.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


okay bye bestie! this page is from perl v5.42.0, dated 2024-10-23, and the footer label is Perl::...utTest(3pm) β€” mwah πŸ’‹βœ¨

πŸ“¦ where this page comes from