line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package MooseX::AttributeHelpers::Trait::Number; |
2
|
22
|
|
|
22
|
|
84
|
use Moose::Role; |
|
22
|
|
|
|
|
31
|
|
|
22
|
|
|
|
|
118
|
|
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
our $VERSION = '0.25'; |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
with 'MooseX::AttributeHelpers::Trait::Base'; |
7
|
|
|
|
|
|
|
|
8
|
2
|
|
|
2
|
1
|
11
|
sub helper_type { 'Num' } |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
# NOTE: |
11
|
|
|
|
|
|
|
# we don't use the method provider for this |
12
|
|
|
|
|
|
|
# module since many of the names of the provied |
13
|
|
|
|
|
|
|
# methods would conflict with keywords |
14
|
|
|
|
|
|
|
# - SL |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
has 'method_constructors' => ( |
17
|
|
|
|
|
|
|
is => 'ro', |
18
|
|
|
|
|
|
|
isa => 'HashRef', |
19
|
|
|
|
|
|
|
lazy => 1, |
20
|
|
|
|
|
|
|
default => sub { |
21
|
|
|
|
|
|
|
return +{ |
22
|
|
|
|
|
|
|
set => sub { |
23
|
|
|
|
|
|
|
my ($attr, $reader, $writer) = @_; |
24
|
8
|
|
|
8
|
|
6478
|
return sub { $writer->($_[0], $_[1]) }; |
25
|
|
|
|
|
|
|
}, |
26
|
|
|
|
|
|
|
add => sub { |
27
|
|
|
|
|
|
|
my ($attr, $reader, $writer) = @_; |
28
|
4
|
|
|
4
|
|
9907
|
return sub { $writer->($_[0], $reader->($_[0]) + $_[1]) }; |
29
|
|
|
|
|
|
|
}, |
30
|
|
|
|
|
|
|
sub => sub { |
31
|
|
|
|
|
|
|
my ($attr, $reader, $writer) = @_; |
32
|
4
|
|
|
4
|
|
1662
|
return sub { $writer->($_[0], $reader->($_[0]) - $_[1]) }; |
33
|
|
|
|
|
|
|
}, |
34
|
|
|
|
|
|
|
mul => sub { |
35
|
|
|
|
|
|
|
my ($attr, $reader, $writer) = @_; |
36
|
2
|
|
|
2
|
|
1492
|
return sub { $writer->($_[0], $reader->($_[0]) * $_[1]) }; |
37
|
|
|
|
|
|
|
}, |
38
|
|
|
|
|
|
|
div => sub { |
39
|
|
|
|
|
|
|
my ($attr, $reader, $writer) = @_; |
40
|
2
|
|
|
2
|
|
1440
|
return sub { $writer->($_[0], $reader->($_[0]) / $_[1]) }; |
41
|
|
|
|
|
|
|
}, |
42
|
|
|
|
|
|
|
mod => sub { |
43
|
|
|
|
|
|
|
my ($attr, $reader, $writer) = @_; |
44
|
4
|
|
|
4
|
|
2083
|
return sub { $writer->($_[0], $reader->($_[0]) % $_[1]) }; |
45
|
|
|
|
|
|
|
}, |
46
|
|
|
|
|
|
|
abs => sub { |
47
|
|
|
|
|
|
|
my ($attr, $reader, $writer) = @_; |
48
|
2
|
|
|
2
|
|
766
|
return sub { $writer->($_[0], abs($reader->($_[0])) ) }; |
49
|
|
|
|
|
|
|
}, |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
); |
53
|
|
|
|
|
|
|
|
54
|
22
|
|
|
22
|
|
89197
|
no Moose::Role; |
|
22
|
|
|
|
|
41
|
|
|
22
|
|
|
|
|
85
|
|
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
1; |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
__END__ |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=pod |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=encoding UTF-8 |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=head1 NAME |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
MooseX::AttributeHelpers::Trait::Number |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=head1 VERSION |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
version 0.25 |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=head1 SYNOPSIS |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
package Real; |
75
|
|
|
|
|
|
|
use Moose; |
76
|
|
|
|
|
|
|
use MooseX::AttributeHelpers; |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
has 'integer' => ( |
79
|
|
|
|
|
|
|
metaclass => 'Number', |
80
|
|
|
|
|
|
|
is => 'ro', |
81
|
|
|
|
|
|
|
isa => 'Int', |
82
|
|
|
|
|
|
|
default => sub { 5 }, |
83
|
|
|
|
|
|
|
provides => { |
84
|
|
|
|
|
|
|
set => 'set', |
85
|
|
|
|
|
|
|
add => 'add', |
86
|
|
|
|
|
|
|
sub => 'sub', |
87
|
|
|
|
|
|
|
mul => 'mul', |
88
|
|
|
|
|
|
|
div => 'div', |
89
|
|
|
|
|
|
|
mod => 'mod', |
90
|
|
|
|
|
|
|
abs => 'abs', |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
); |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
my $real = Real->new(); |
95
|
|
|
|
|
|
|
$real->add(5); # same as $real->integer($real->integer + 5); |
96
|
|
|
|
|
|
|
$real->sub(2); # same as $real->integer($real->integer - 2); |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=head1 DESCRIPTION |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
This provides a simple numeric attribute, which supports most of the |
101
|
|
|
|
|
|
|
basic math operations. |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=head1 METHODS |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=over 4 |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=item B<meta> |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=item B<helper_type> |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=item B<method_constructors> |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=back |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=head1 PROVIDED METHODS |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
It is important to note that all those methods do in place |
118
|
|
|
|
|
|
|
modification of the value stored in the attribute. |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=over 4 |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=item I<set ($value)> |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
Alternate way to set the value. |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=item I<add ($value)> |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
Adds the current value of the attribute to C<$value>. |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=item I<sub ($value)> |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
Subtracts the current value of the attribute to C<$value>. |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=item I<mul ($value)> |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
Multiplies the current value of the attribute to C<$value>. |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=item I<div ($value)> |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
Divides the current value of the attribute to C<$value>. |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=item I<mod ($value)> |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
Modulus the current value of the attribute to C<$value>. |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=item I<abs> |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
Sets the current value of the attribute to its absolute value. |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=back |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=head1 SUPPORT |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=MooseX-AttributeHelpers> |
155
|
|
|
|
|
|
|
(or L<bug-MooseX-AttributeHelpers@rt.cpan.org|mailto:bug-MooseX-AttributeHelpers@rt.cpan.org>). |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
There is also a mailing list available for users of this distribution, at |
158
|
|
|
|
|
|
|
L<http://lists.perl.org/list/moose.html>. |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
There is also an irc channel available for users of this distribution, at |
161
|
|
|
|
|
|
|
L<C<#moose> on C<irc.perl.org>|irc://irc.perl.org/#moose>. |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=head1 AUTHOR |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
Stevan Little <stevan@iinteractive.com> |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
Robert Boone |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
This software is copyright (c) 2007 by Stevan Little and Infinity Interactive, Inc. |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
174
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=cut |