line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Finance::Random::Price; |
2
|
|
|
|
|
|
|
|
3
|
4
|
|
|
4
|
|
91806
|
use strict; |
|
4
|
|
|
|
|
24
|
|
|
4
|
|
|
|
|
144
|
|
4
|
4
|
|
|
4
|
|
20
|
use warnings; |
|
4
|
|
|
|
|
8
|
|
|
4
|
|
|
|
|
112
|
|
5
|
|
|
|
|
|
|
|
6
|
4
|
|
|
4
|
|
2022
|
use Class::Utils qw(set_params); |
|
4
|
|
|
|
|
48032
|
|
|
4
|
|
|
|
|
77
|
|
7
|
4
|
|
|
4
|
|
2453
|
use Data::Currency; |
|
4
|
|
|
|
|
430231
|
|
|
4
|
|
|
|
|
214
|
|
8
|
4
|
|
|
4
|
|
42
|
use Error::Pure qw(err); |
|
4
|
|
|
|
|
8
|
|
|
4
|
|
|
|
|
2227
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = 0.01; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
# Constructor. |
13
|
|
|
|
|
|
|
sub new { |
14
|
14
|
|
|
14
|
1
|
14119
|
my ($class, @params) = @_; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
# Create object. |
17
|
14
|
|
|
|
|
48
|
my $self = bless {}, $class; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
# Price currencies. |
20
|
14
|
|
|
|
|
50
|
$self->{'currencies'} = ['CZK']; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
# Decimal numbers. |
23
|
14
|
|
|
|
|
27
|
$self->{'decimal_num'} = undef; |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
# Min/max value.. |
26
|
14
|
|
|
|
|
25
|
$self->{'min'} = 0; |
27
|
14
|
|
|
|
|
25
|
$self->{'max'} = 10000; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
# Process params. |
30
|
14
|
|
|
|
|
46
|
set_params($self, @params); |
31
|
|
|
|
|
|
|
|
32
|
12
|
100
|
|
|
|
187
|
if (! defined $self->{'min'}) { |
33
|
1
|
|
|
|
|
16
|
err "Parameter 'min' is required."; |
34
|
|
|
|
|
|
|
} |
35
|
11
|
100
|
|
|
|
89
|
if ($self->{'min'} !~ m/^\-?\d+\.?\d*$/ms) { |
36
|
1
|
|
|
|
|
11
|
err "Parameter 'min' must be a number."; |
37
|
|
|
|
|
|
|
} |
38
|
10
|
100
|
|
|
|
31
|
if (! defined $self->{'max'}) { |
39
|
1
|
|
|
|
|
12
|
err "Parameter 'max' is required."; |
40
|
|
|
|
|
|
|
} |
41
|
9
|
100
|
|
|
|
33
|
if ($self->{'max'} !~ m/^\-?\d+\.?\d*$/ms) { |
42
|
1
|
|
|
|
|
4
|
err "Parameter 'max' must be a number."; |
43
|
|
|
|
|
|
|
} |
44
|
8
|
100
|
|
|
|
25
|
if ($self->{'max'} < $self->{'min'}) { |
45
|
1
|
|
|
|
|
4
|
err "Parameter 'max' must be greater than parameter 'min'."; |
46
|
|
|
|
|
|
|
} |
47
|
7
|
100
|
|
|
|
18
|
if (defined $self->{'decimal_num'}) { |
48
|
3
|
100
|
|
|
|
23
|
if ($self->{'decimal_num'} !~ m/^\-?\d+$/ms) { |
49
|
1
|
|
|
|
|
11
|
err "Parameter 'decimal_num' must be a number."; |
50
|
|
|
|
|
|
|
} |
51
|
2
|
100
|
|
|
|
7
|
if ($self->{'decimal_num'} < 0) { |
52
|
1
|
|
|
|
|
4
|
err "Parameter 'decimal_num' must be greater than 0."; |
53
|
|
|
|
|
|
|
} |
54
|
1
|
50
|
|
|
|
4
|
if ($self->{'decimal_num'} > 2) { |
55
|
1
|
|
|
|
|
5
|
err "Parameter 'decimal_num' must be lesser than 3."; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
# Object. |
60
|
4
|
|
|
|
|
19
|
return $self; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub random { |
64
|
3
|
|
|
3
|
1
|
13
|
my $self = shift; |
65
|
|
|
|
|
|
|
|
66
|
3
|
50
|
|
|
|
19
|
my $dec_mul = $self->{'decimal_num'} ? $self->{'decimal_num'} * 10 : 1; |
67
|
|
|
|
|
|
|
|
68
|
3
|
|
|
|
|
6
|
my $rand_currency = int(rand(scalar @{$self->{'currencies'}})); |
|
3
|
|
|
|
|
44
|
|
69
|
|
|
|
|
|
|
my $rand_value = int(rand(($self->{'max'} - $self->{'min'} + 1) * $dec_mul)) |
70
|
3
|
|
|
|
|
16
|
/ $dec_mul + $self->{'min'}; |
71
|
|
|
|
|
|
|
|
72
|
3
|
|
|
|
|
17
|
my $price = Data::Currency->new($rand_value, $self->{'currencies'}->[$rand_currency]); |
73
|
|
|
|
|
|
|
|
74
|
3
|
|
|
|
|
327
|
return $price; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
1; |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
__END__ |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=pod |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=encoding utf8 |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=head1 NAME |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
Finance::Random::Price - Perl class for creating random image. |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=head1 SYNOPSIS |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
use Finance::Random::Price; |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
my $obj = Finance::Random::Price->new(%parameters); |
94
|
|
|
|
|
|
|
my $price = $obj->random; |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=head1 METHODS |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=head2 C<new> |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
my $obj = Finance::Random::Price->new(%parameters); |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
Constructor. |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=over 8 |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=item * C<currencies> |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
Reference to array with possible currencies. |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
Default value is ['CZK']. |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=item * C<decimal_num> |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
Number of decimal characters in number. Possible values are undef, 1 or 2. |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
Default value is undef. |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=item * C<min> |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
Minimal value for random price. |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
Default value is 0. |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=item * C<max> |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
Maximal value for random price. |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
Default value is 100. |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=back |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
Returns instance of object. |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=head2 C<random> |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
my $price = $obj->random; |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
Get random price. |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
Returns Data::Currency object. |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=head1 ERRORS |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
new(): |
145
|
|
|
|
|
|
|
Parameter 'decimal_num' must be a number. |
146
|
|
|
|
|
|
|
Parameter 'decimal_num' must be greater than 0. |
147
|
|
|
|
|
|
|
Parameter 'decimal_num' must be lesser than 3. |
148
|
|
|
|
|
|
|
Parameter 'min' is required. |
149
|
|
|
|
|
|
|
Parameter 'min' must be a number. |
150
|
|
|
|
|
|
|
Parameter 'max' is required. |
151
|
|
|
|
|
|
|
Parameter 'max' must be a number. |
152
|
|
|
|
|
|
|
Parameter 'max' must be greater than parameter 'min'. |
153
|
|
|
|
|
|
|
From Class::Utils: |
154
|
|
|
|
|
|
|
Unknown parameter '%s'. |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=head1 EXAMPLE |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=for comment filename=random_usd_or_eur.pl |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
use strict; |
161
|
|
|
|
|
|
|
use warnings; |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
use Finance::Random::Price; |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
# Object. |
166
|
|
|
|
|
|
|
my $obj = Finance::Random::Price->new( |
167
|
|
|
|
|
|
|
'currencies' => ['USD', 'EUR'], |
168
|
|
|
|
|
|
|
'min' => 99, |
169
|
|
|
|
|
|
|
'max' => 101, |
170
|
|
|
|
|
|
|
); |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
# Print random price. |
173
|
|
|
|
|
|
|
print $obj->random."\n"; |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
# Output like: |
176
|
|
|
|
|
|
|
# EUR100,00 |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
# Output like: |
179
|
|
|
|
|
|
|
# $99.00 |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
# Output like: |
182
|
|
|
|
|
|
|
# $101.00 |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=head1 DEPENDENCIES |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
L<Class::Utils>, |
187
|
|
|
|
|
|
|
L<Data::Currency>, |
188
|
|
|
|
|
|
|
L<Error::Pure>. |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
=head1 REPOSITORY |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
L<https://github.com/michal-josef-spacek/Finance-Random-Price>. |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=head1 AUTHOR |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
Michal Josef Špaček L<mailto:skim@cpan.org> |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
L<http://skim.cz> |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
© 2023 Michal Josef Špaček |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
BSD 2-Clause License |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
=head1 VERSION |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
0.01 |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
=cut |