File Coverage

blib/lib/Perl/Critic/Policy/ValuesAndExpressions/RequireNumberSeparators.pm
Criterion Covered Total %
statement 27 27 100.0
branch 6 6 100.0
condition n/a
subroutine 11 11 100.0
pod 4 5 80.0
total 48 49 97.9


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::ValuesAndExpressions::RequireNumberSeparators;
2              
3 40     40   26613 use 5.010001;
  40         167  
4 40     40   265 use strict;
  40         95  
  40         879  
5 40     40   203 use warnings;
  40         98  
  40         1078  
6 40     40   238 use Readonly;
  40         113  
  40         2184  
7              
8 40     40   301 use Perl::Critic::Utils qw{ :severities };
  40         128  
  40         2188  
9 40     40   5198 use parent 'Perl::Critic::Policy';
  40         114  
  40         248  
10              
11             our $VERSION = '1.146';
12              
13             #-----------------------------------------------------------------------------
14              
15             Readonly::Scalar my $DESC => q{Long number not separated with underscores};
16             Readonly::Scalar my $EXPL => [ 59 ];
17              
18             #-----------------------------------------------------------------------------
19              
20             Readonly::Scalar my $MINIMUM_INTEGER_WITH_MULTIPLE_DIGITS => 10;
21              
22             sub supported_parameters {
23             return (
24             {
25 95     95 0 2097 name => 'min_value',
26             description => 'The minimum absolute value to require separators in.',
27             default_string => '10_000',
28             behavior => 'integer',
29             integer_minimum => $MINIMUM_INTEGER_WITH_MULTIPLE_DIGITS,
30             },
31             );
32             }
33              
34 109     109 1 540 sub default_severity { return $SEVERITY_LOW }
35 84     84 1 402 sub default_themes { return qw( core pbp cosmetic ) }
36 35     35 1 154 sub applies_to { return 'PPI::Token::Number' }
37              
38             #-----------------------------------------------------------------------------
39              
40             sub violates {
41 128     128 1 336 my ( $self, $elem, undef ) = @_;
42              
43 128 100       598 return if $elem->isa( 'PPI::Token::Number::Version' ); # GitHub #856
44              
45 127         310 my $min = $self->{_min_value};
46              
47 127 100       332 return if $elem !~ m{ \d{4} }xms;
48 42 100       416 return if abs $elem->literal() < $min;
49              
50 35         821 return $self->violation( $DESC, $EXPL, $elem );
51             }
52              
53             1;
54              
55             __END__
56              
57             #-----------------------------------------------------------------------------
58              
59             =pod
60              
61             =head1 NAME
62              
63             Perl::Critic::Policy::ValuesAndExpressions::RequireNumberSeparators - Write C< 141_234_397.0145 > instead of C< 141234397.0145 >.
64              
65              
66             =head1 AFFILIATION
67              
68             This Policy is part of the core L<Perl::Critic|Perl::Critic>
69             distribution.
70              
71              
72             =head1 DESCRIPTION
73              
74             Long numbers can be difficult to read. To improve legibility, Perl
75             allows numbers to be split into groups of digits separated by
76             underscores. This policy requires number sequences of more than three
77             digits to be separated.
78              
79             $long_int = 123456789; #not ok
80             $long_int = 123_456_789; #ok
81              
82             $long_float = 12345678.001; #not ok
83             $long_float = 12_345_678.001; #ok
84              
85             =head1 CONFIGURATION
86              
87             The minimum absolute value of numbers that must contain separators can
88             be configured via the C<min_value> option. The default is 10,000;
89             thus, all numbers >= 10,000 and <= -10,000 must have separators. For
90             example:
91              
92             [ValuesAndExpressions::RequireNumberSeparators]
93             min_value = 100000 # That's one-hundred-thousand!
94              
95             =head1 NOTES
96              
97             As it is currently written, this policy only works properly with
98             decimal (base 10) numbers. And it is obviously biased toward Western
99             notation. I'll try and address those issues in the future.
100              
101             =head1 AUTHOR
102              
103             Jeffrey Ryan Thalhammer <jeff@imaginative-software.com>
104              
105             =head1 COPYRIGHT
106              
107             Copyright (c) 2005-2011 Imaginative Software Systems. All rights reserved.
108              
109             This program is free software; you can redistribute it and/or modify
110             it under the same terms as Perl itself. The full text of this license
111             can be found in the LICENSE file included with this module.
112              
113             =cut
114              
115             # Local Variables:
116             # mode: cperl
117             # cperl-indent-level: 4
118             # fill-column: 78
119             # indent-tabs-mode: nil
120             # c-indentation-style: bsd
121             # End:
122             # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :