1 ##############################################################################
2 # $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/lib/Perl/Critic/Policy/ValuesAndExpressions/RequireNumberSeparators.pm $
3 # $Date: 2008-07-03 10:19:10 -0500 (Thu, 03 Jul 2008) $
6 ##############################################################################
8 package Perl::Critic::Policy::ValuesAndExpressions::RequireNumberSeparators;
15 use Perl::Critic::Utils qw{ :severities };
16 use base 'Perl::Critic::Policy';
18 our $VERSION = '1.088';
20 #-----------------------------------------------------------------------------
22 Readonly::Scalar my $DESC => q{Long number not separated with underscores};
23 Readonly::Scalar my $EXPL => [ 59 ];
25 #-----------------------------------------------------------------------------
27 Readonly::Scalar my $MINIMUM_INTEGER_WITH_MULTIPLE_DIGITS => 10;
29 sub supported_parameters {
33 description => 'The minimum absolute value to require separators in.',
34 default_string => '10_000',
35 behavior => 'integer',
36 integer_minimum => $MINIMUM_INTEGER_WITH_MULTIPLE_DIGITS,
41 sub default_severity { return $SEVERITY_LOW }
42 sub default_themes { return qw( core pbp cosmetic ) }
43 sub applies_to { return 'PPI::Token::Number' }
45 #-----------------------------------------------------------------------------
48 my ( $self, $elem, undef ) = @_;
49 my $min = $self->{_min_value};
51 return if $elem !~ m{ \d{4} }mx;
52 return if abs $elem->literal() < $min;
54 return $self->violation( $DESC, $EXPL, $elem );
61 #-----------------------------------------------------------------------------
67 Perl::Critic::Policy::ValuesAndExpressions::RequireNumberSeparators - Write C< 141_234_397.0145 > instead of C< 141234397.0145 >.
71 This Policy is part of the core L<Perl::Critic> distribution.
76 Long numbers can be difficult to read. To improve legibility, Perl
77 allows numbers to be split into groups of digits separated by
78 underscores. This policy requires number sequences of more than
79 three digits to be separated.
81 $long_int = 123456789; #not ok
82 $long_int = 123_456_789; #ok
84 $long_float = 12345678.001; #not ok
85 $long_float = 12_345_678.001; #ok
89 The minimum absolute value of numbers that must contain separators can
90 be configured via the C<min_value> option. The default is 10,000;
91 thus, all numbers >= 10,000 and <= -10,000 must have separators. For
94 [ValuesAndExpressions::RequireNumberSeparators]
95 min_value = 100000 # That's one-hundred-thousand!
99 As it is currently written, this policy only works properly with
100 decimal (base 10) numbers. And it is obviously biased toward Western
101 notation. I'll try and address those issues in the future.
105 Jeffrey Ryan Thalhammer <thaljef@cpan.org>
109 Copyright (c) 2005-2008 Jeffrey Ryan Thalhammer. All rights reserved.
111 This program is free software; you can redistribute it and/or modify
112 it under the same terms as Perl itself. The full text of this license
113 can be found in the LICENSE file included with this module.
119 # cperl-indent-level: 4
121 # indent-tabs-mode: nil
122 # c-indentation-style: bsd
124 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :