Perl::Critic::Policy::Plicease::ProhibitUnicodeDigitInRegexp - Prohibit non-ASCII \d in regular expressions
version 0.05
perlcriticrc:
[Plicease::ProhibitUnicodeDigitInRegexp]
code:
/\d/; # not ok /[0-9]/; # ok
The character class \d in a regular expression matches all unicode digit character, which might not be what you expect if you are testing if a string can be used as a number in Perl. Instead use either [0-9], or if you are on Perl 5.14 or better you can use the /a modifier. This policy allows \d in expressions with an explicit /u modifier (normally on by default), as it indicates that the code is expecting Unicode semantics, including Unicode digits.
\d
[0-9]
/a
/u
/\d/; # not ok /\d/a; # ok /\d/u; # ok /[0-9]/; # ok
None.
This policy is not configurable except for the standard options.
This is not a general policy, and should not be applied toward all applications without some thought. This is frequently true for Perl::Critic policies, but especially so for this policy.
In the general the ability to match against unicode digits is a useful ability, and doesn't constitute bad code. On the other hand, some applications don't ever need to match non-ASCII digit characters, and incorrectly rely on \d to validate as a number as Perl understands it (and Perl understands non-ASCII digits as zero regardless of what they mean in their respective languages).
This policy doesn't take into account using the re pragma.
use re '/a'; /\d/; # (still) not ok
Author: Graham Ollis <plicease@cpan.org>
Contributors:
Ville Skyttä (SCOP)
This software is copyright (c) 2019-2024 by Graham Ollis.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Perl::Critic::Plicease, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Perl::Critic::Plicease
CPAN shell
perl -MCPAN -e shell install Perl::Critic::Plicease
For more information on module installation, please visit the detailed CPAN module installation guide.