1 ##############################################################################
2 # $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/lib/Perl/Critic/PolicyParameter/Behavior/Integer.pm $
3 # $Date: 2008-07-03 10:19:10 -0500 (Thu, 03 Jul 2008) $
6 ##############################################################################
8 package Perl::Critic::PolicyParameter::Behavior::Integer;
14 use Perl::Critic::Utils qw{ :characters };
16 use base qw{ Perl::Critic::PolicyParameter::Behavior };
18 our $VERSION = '1.088';
20 #-----------------------------------------------------------------------------
22 sub initialize_parameter {
23 my ($self, $parameter, $specification) = @_;
25 my $minimum = $specification->{integer_minimum};
26 my $maximum = $specification->{integer_maximum};
28 $parameter->_get_behavior_values()->{minimum} = $minimum;
29 $parameter->_get_behavior_values()->{maximum} = $maximum;
31 $parameter->_set_parser(
33 # Normally bad thing, obscuring a variable in a outer scope
34 # with a variable with the same name is being done here in
35 # order to remain consistent with the parser function interface.
36 my ($policy, $parameter, $config_string) = @_;
38 my $value_string = $parameter->get_default_string();
40 if (defined $config_string) {
41 $value_string = $config_string;
45 if ( defined $value_string ) {
47 $value_string !~ m/ \A [-+]? [1-9] [\d_]* \z /xms
48 and $value_string ne '0'
50 $policy->throw_parameter_value_exception(
51 $parameter->get_name(),
54 'does not look like an integer.',
58 $value_string =~ tr/_//d;
59 $value = $value_string + 0;
61 if ( defined $minimum and $minimum > $value ) {
62 $policy->throw_parameter_value_exception(
63 $parameter->get_name(),
66 qq{is less than $minimum.},
70 if ( defined $maximum and $maximum < $value ) {
71 $policy->throw_parameter_value_exception(
72 $parameter->get_name(),
75 qq{is greater than $maximum.},
80 $policy->__set_parameter_value($parameter, $value);
88 #-----------------------------------------------------------------------------
90 sub generate_parameter_description {
91 my ($self, $parameter) = @_;
93 my $minimum = $parameter->_get_behavior_values()->{minimum};
94 my $maximum = $parameter->_get_behavior_values()->{maximum};
96 my $description = $parameter->_get_description_with_trailing_period();
98 $description .= qq{\n};
101 if (defined $minimum or defined $maximum) {
102 if (defined $minimum) {
103 $description .= "Minimum value $minimum. ";
105 $description .= 'No minimum. ';
108 if (defined $maximum) {
109 $description .= "Maximum value $maximum.";
111 $description .= 'No maximum.';
114 $description .= 'No limits.';
120 #-----------------------------------------------------------------------------
126 #-----------------------------------------------------------------------------
134 Perl::Critic::PolicyParameter::Behavior::Integer - Actions appropriate for an integer parameter.
139 Provides a standard set of functionality for an integer
140 L<Perl::Critic::PolicyParameter> so that the developer of a policy
141 does not have to provide it her/himself.
143 The parser provided by this behavior allows underscores ("_") in input
144 values as in a Perl numeric literal.
146 NOTE: Do not instantiate this class. Use the singleton instance held
147 onto by L<Perl::Critic::PolicyParameter>.
154 =item C<initialize_parameter( $parameter, $specification )>
156 Plug in the functionality this behavior provides into the parameter,
157 based upon the configuration provided by the specification.
159 This behavior looks for two configuration items:
163 =item integer_minimum
165 Optional. The minimum acceptable value. Inclusive.
167 =item integer_maximum
169 Optional. The maximum acceptable value. Inclusive.
173 =item C<generate_parameter_description( $parameter )>
175 Create a description of the parameter, based upon the description on
176 the parameter itself, but enhancing it with information from this
179 In this case, this means including the minimum and maximum values.
186 Elliot Shank <perl@galumph.com>
190 Copyright (c) 2007-2008 Elliot Shank. All rights reserved.
192 This program is free software; you can redistribute it and/or modify
193 it under the same terms as Perl itself. The full text of this license
194 can be found in the LICENSE file included with this module.
200 # cperl-indent-level: 4
202 # indent-tabs-mode: nil
203 # c-indentation-style: bsd
205 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :