File Coverage

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


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::ValuesAndExpressions::RequireNumberSeparators;
2              
3 40     40   25902 use 5.010001;
  40         157  
4 40     40   226 use strict;
  40         97  
  40         814  
5 40     40   196 use warnings;
  40         90  
  40         943  
6 40     40   228 use Readonly;
  40         95  
  40         2069  
7              
8 40     40   266 use Perl::Critic::Utils qw{ :severities };
  40         84  
  40         2056  
9 40     40   5101 use parent 'Perl::Critic::Policy';
  40         108  
  40         226  
10              
11             our $VERSION = '1.150';
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 90     90 0 2026 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 88     88 1 346 sub default_severity { return $SEVERITY_LOW }
35 84     84 1 328 sub default_themes { return qw( core pbp cosmetic ) }
36 30     30 1 79 sub applies_to { return 'PPI::Token::Number' }
37              
38             #-----------------------------------------------------------------------------
39              
40             sub violates {
41 78     78 1 153 my ( $self, $elem, undef ) = @_;
42              
43 78 50       294 return if $elem->isa( 'PPI::Token::Number::Version' ); # GitHub #856
44              
45 78         159 my $min = $self->{_min_value};
46              
47 78 100       164 return if $elem !~ m{ \d{4} }xms;
48 14 50       170 return if abs $elem->literal() < $min;
49              
50 14         292 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 :