NAME
Perl::Critic::Policy::RegularExpressions::ProhibitCaptureWithoutTest - Capture variable used outside conditional.
AFFILIATION
This Policy is part of the core Perl::Critic distribution.
DESCRIPTION
If a regexp match fails, then any capture variables ($1
, $2
, ...) will be undefined. Therefore it's important to check the return value of a match before using those variables.
This policy checks that the previous regexp for which the capture variable is in-scope is either in a conditional or causes an exception if the match fails.
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.
CONFIGURATION
By default, this policy considers die
, croak
, and confess
to throw exceptions. If you have additional routines 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
BUGS
Needs to allow this construct:
for ( ... ) {
next unless /(....)/;
if ( $1 ) {
....
}
}
Right now, Perl::Critic thinks that the $1
isn't legal to use because it's "outside" of the match. The thing is, we can only get to the if
if the regex matched.
while ( $str =~ /(expression)/ )
This policy does not recognize named capture variables. Yet.
AUTHOR
Chris Dolan <cdolan@cpan.org>
COPYRIGHT
Copyright (c) 2006-2009 Chris Dolan.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.