« blib/lib/Perl/Critic/Policy/ControlStructures/ProhibitDeepNests.pm
↑ Summary
blib/lib/Perl/Critic/Policy/Modules/RequireEndWithOne.pm »
| 1 | package Perl::Critic::Policy::ValuesAndExpressions::RequireNumberSeparators; | ||||||||||
| 2 | |||||||||||
| 3 | 40 | ▶ | use 5.010001; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 40 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 40 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 40 | ▶ | use Readonly; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 40 | ▶ | use Perl::Critic::Utils qw{ :severities }; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 40 | ▶ | use parent 'Perl::Critic::Policy'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 40 | our $VERSION = '1.150'; | |||||||||
| 12 | |||||||||||
| 13 | #----------------------------------------------------------------------------- | ||||||||||
| 14 | |||||||||||
| 15 | 40 | Readonly::Scalar my $DESC => q{Long number not separated with underscores}; | |||||||||
| 16 | 40 | Readonly::Scalar my $EXPL => [ 59 ]; | |||||||||
| 17 | |||||||||||
| 18 | #----------------------------------------------------------------------------- | ||||||||||
| 19 | |||||||||||
| 20 | 40 | Readonly::Scalar my $MINIMUM_INTEGER_WITH_MULTIPLE_DIGITS => 10; | |||||||||
| 21 | |||||||||||
| 22 | sub supported_parameters { | ||||||||||
| 23 | return ( | ||||||||||
| 24 | { | ||||||||||
| 25 | 91 | ▶ | name => 'min_value', | ||||||||
|
Subroutine
supported_parameters: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 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 | 89 | ▶ | sub default_severity { return $SEVERITY_LOW } | ||||||||
|
Subroutine
default_severity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 35 | 84 | ▶ | sub default_themes { return qw( core pbp cosmetic ) } | ||||||||
|
Subroutine
default_themes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 36 | 31 | ▶ | sub applies_to { return 'PPI::Token::Number' } | ||||||||
|
Subroutine
applies_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 37 | |||||||||||
| 38 | #----------------------------------------------------------------------------- | ||||||||||
| 39 | |||||||||||
| 40 | sub violates { | ||||||||||
| 41 | 78 | ▶ | my ( $self, $elem, undef ) = @_; | ||||||||
|
Subroutine
violates: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||
| 42 | |||||||||||
| 43 | 78 | ▶ | return if $elem->isa( 'PPI::Token::Number::Version' ); # GitHub #856 | ||||||||
|
Branch: 1 branch
| |||||||||||
| 44 | |||||||||||
| 45 | 78 | my $min = $self->{_min_value}; | |||||||||
| 46 | |||||||||||
| 47 | 78 | ▶ | return if $elem !~ m{ \d{4} }xms; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 48 | 14 | ▶ | return if abs $elem->literal() < $min; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | |||||||||||
| 50 | 14 | return $self->violation( $DESC, $EXPL, $elem ); | |||||||||
| 51 | } | ||||||||||
| 52 | |||||||||||
| 53 | 40 | 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 : | ||||||||||