1package MooseX::Types::Common::Numeric;
2# ABSTRACT: Commonly used numeric types
3
4our $VERSION = '0.001014';
5
6use strict;
7use warnings;
8
9use MooseX::Types -declare => [
10  qw(PositiveNum PositiveOrZeroNum
11     PositiveInt PositiveOrZeroInt
12     NegativeNum NegativeOrZeroNum
13     NegativeInt NegativeOrZeroInt
14     SingleDigit)
15];
16
17use MooseX::Types::Moose qw/Num Int/;
18use if MooseX::Types->VERSION >= 0.42, 'namespace::autoclean';
19
20subtype PositiveNum,
21  as Num,
22  where { $_ > 0 },
23  message { "Must be a positive number" },
24    ( $Moose::VERSION >= 2.0200
25        ? inline_as {
26            $_[0]->parent()->_inline_check( $_[1] ) . ' && '
27                . qq{ ($_[1] > 0) };
28        }
29        : ()
30    );
31
32subtype PositiveOrZeroNum,
33  as Num,
34  where { $_ >= 0 },
35  message { "Must be a number greater than or equal to zero" },
36    ( $Moose::VERSION >= 2.0200
37        ? inline_as {
38            $_[0]->parent()->_inline_check( $_[1] ) . ' && '
39                . qq{ ($_[1] >= 0) };
40        }
41        : ()
42    );
43
44subtype PositiveInt,
45  as Int,
46  where { $_ > 0 },
47  message { "Must be a positive integer" },
48    ( $Moose::VERSION >= 2.0200
49        ? inline_as {
50            $_[0]->parent()->_inline_check( $_[1] ) . ' && '
51                . qq{ ($_[1] > 0) };
52        }
53        : ()
54    );
55
56subtype PositiveOrZeroInt,
57  as Int,
58  where { $_ >= 0 },
59  message { "Must be an integer greater than or equal to zero" },
60    ( $Moose::VERSION >= 2.0200
61        ? inline_as {
62            $_[0]->parent()->_inline_check( $_[1] ) . ' && '
63                . qq{ ($_[1] >= 0) };
64        }
65        : ()
66    );
67
68subtype NegativeNum,
69  as Num,
70  where { $_ < 0 },
71  message { "Must be a negative number" },
72    ( $Moose::VERSION >= 2.0200
73        ? inline_as {
74            $_[0]->parent()->_inline_check( $_[1] ) . ' && '
75                . qq{ ($_[1] < 0) };
76        }
77        : ()
78    );
79
80subtype NegativeOrZeroNum,
81  as Num,
82  where { $_ <= 0 },
83  message { "Must be a number less than or equal to zero" },
84    ( $Moose::VERSION >= 2.0200
85        ? inline_as {
86            $_[0]->parent()->_inline_check( $_[1] ) . ' && '
87                . qq{ ($_[1] <= 0) };
88        }
89        : ()
90    );
91
92subtype NegativeInt,
93  as Int,
94  where { $_ < 0 },
95  message { "Must be a negative integer" },
96    ( $Moose::VERSION >= 2.0200
97        ? inline_as {
98            $_[0]->parent()->_inline_check( $_[1] ) . ' && '
99                . qq{ ($_[1] < 0) };
100        }
101        : ()
102    );
103
104subtype NegativeOrZeroInt,
105  as Int,
106  where { $_ <= 0 },
107  message { "Must be an integer less than or equal to zero" },
108    ( $Moose::VERSION >= 2.0200
109        ? inline_as {
110            $_[0]->parent()->_inline_check( $_[1] ) . ' && '
111                . qq{ ($_[1] <= 0) };
112        }
113        : ()
114    );
115
116subtype SingleDigit,
117  as Int,
118  where { $_ >= -9 and $_ <= 9 },
119  message { "Must be a single digit" },
120    ( $Moose::VERSION >= 2.0200
121        ? inline_as {
122            $_[0]->parent()->_inline_check( $_[1] ) . ' && '
123                . qq{ ($_[1] >= -9 and $_[1] <= 9) };
124        }
125        : ()
126    );
127
1281;
129
130__END__
131
132=pod
133
134=encoding UTF-8
135
136=head1 NAME
137
138MooseX::Types::Common::Numeric - Commonly used numeric types
139
140=head1 VERSION
141
142version 0.001014
143
144=head1 SYNOPSIS
145
146    use MooseX::Types::Common::Numeric qw/PositiveInt/;
147    has count => (is => 'rw', isa => PositiveInt);
148
149    ...
150    #this will fail
151    $object->count(-33);
152
153=head1 DESCRIPTION
154
155A set of commonly-used numeric type constraints that do not ship with Moose by
156default.
157
158=over
159
160=item * C<PositiveNum>
161
162=item * C<PositiveOrZeroNum>
163
164=item * C<PositiveInt>
165
166=item * C<PositiveOrZeroInt>
167
168=item * C<NegativeNum>
169
170=item * C<NegativeOrZeroNum>
171
172=item * C<NegativeInt>
173
174=item * C<NegativeOrZeroInt>
175
176=item * C<SingleDigit>
177
178=back
179
180=head1 SEE ALSO
181
182=over
183
184=item * L<MooseX::Types::Common::String>
185
186=back
187
188=head1 SUPPORT
189
190Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=MooseX-Types-Common>
191(or L<bug-MooseX-Types-Common@rt.cpan.org|mailto:bug-MooseX-Types-Common@rt.cpan.org>).
192
193There is also a mailing list available for users of this distribution, at
194L<http://lists.perl.org/list/moose.html>.
195
196There is also an irc channel available for users of this distribution, at
197L<C<#moose> on C<irc.perl.org>|irc://irc.perl.org/#moose>.
198
199=head1 AUTHORS
200
201=over 4
202
203=item *
204
205Matt S Trout - mst (at) shadowcatsystems.co.uk (L<http://www.shadowcatsystems.co.uk/>)
206
207=item *
208
209K. James Cheetham <jamie@shadowcatsystems.co.uk>
210
211=item *
212
213Guillermo Roditi <groditi@gmail.com>
214
215=back
216
217=head1 COPYRIGHT AND LICENSE
218
219This software is copyright (c) 2009 by Matt S Trout - mst (at) shadowcatsystems.co.uk (L<http://www.shadowcatsystems.co.uk/>).
220
221This is free software; you can redistribute it and/or modify it under
222the same terms as the Perl 5 programming language system itself.
223
224=cut
225