Perl::Critic::Policy::RegularExpressions::ProhibitCompleUsereContributed PerlPerl::Critic::Policy::RegularExpressions::ProhibitComplexRegexes(3pm)
NAME
Perl::Critic::Policy::RegularExpressions::ProhibitComplexRegexes - Split long regexps into smaller "qr//" chunks.
AFFILIATION
This Policy is part of the core Perl::Critic distribution.
DESCRIPTION
Big regexps are hard to read, perhaps even the hardest part of Perl. A good practice to write digestible chunks of regexp and put them
together. This policy flags any regexp that is longer than "N" characters, where "N" is a configurable value that defaults to 60. If the
regexp uses the "x" flag, then the length is computed after parsing out any comments or whitespace.
Unfortunately the use of descriptive (and therefore longish) variable names can cause regexps to be in violation of this policy, so
interpolated variables are counted as 4 characters no matter how long their names actually are.
CASE STUDY
As an example, look at the regexp used to match email addresses in Email::Valid::Loose (tweaked lightly to wrap for POD)
(?x-ism:(?:[^( 40)<>@,;:".\[] 00- 37x80-xff]+(?![^( 40)<>@,;:".\[]
00- 37x80-xff])|"[^\x80-xff
15"]*(?:\[^x80-xff][^\x80-xff
15
"]*)*")(?:(?:[^( 40)<>@,;:".\[] 00- 37x80-xff]+(?![^( 40)<>@,;:".\[
] 00- 37x80-xff])|"[^\x80-xff
15"]*(?:\[^x80-xff][^\x80-xff
15"]*)*")|.)*@(?:[^( 40)<>@,;:".\[] 00- 37x80-xff]+(?![^( 40)<>@,
;:".\[] 00- 37x80-xff])|[(?:[^\x80-xff
15[]]|\[^x80-xff])*]
)(?:.(?:[^( 40)<>@,;:".\[] 00- 37x80-xff]+(?![^( 40)<>@,;:".\[] 00
- 37x80-xff])|[(?:[^\x80-xff
15[]]|\[^x80-xff])*]))*)
which is constructed from the following code:
my $esc = '\\';
my $period = '.';
my $space = '