line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package HTML::CheckArgs::dollar; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# to-do: check for alpha chars and throw and error instead of stripping |
4
|
|
|
|
|
|
|
# them out first? |
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
34
|
|
7
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
63
|
|
8
|
|
|
|
|
|
|
|
9
|
1
|
|
|
1
|
|
8
|
use base 'HTML::CheckArgs::Object'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
717
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub is_valid { |
12
|
15
|
|
|
15
|
0
|
19
|
my $self = shift; |
13
|
|
|
|
|
|
|
|
14
|
15
|
|
|
|
|
44
|
my $value = $self->value; |
15
|
15
|
|
|
|
|
42
|
my $config = $self->config; |
16
|
|
|
|
|
|
|
|
17
|
15
|
|
|
|
|
72
|
$self->check_params( required => [], optional => [ qw( min max ) ], cleanable => 1 ); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
# no value passed in |
20
|
|
|
|
|
|
|
# zero is a valid dollar, so we can't just check !$value |
21
|
15
|
100
|
100
|
|
|
136
|
if ( $config->{required} && ( !defined( $value ) || $value eq '' ) ) { |
|
|
100
|
66
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
|
66
|
|
|
|
|
22
|
2
|
|
|
|
|
10
|
$self->error_code( 'dollar_00' ); # required |
23
|
2
|
|
|
|
|
8
|
$self->error_message( 'Not given.' ); |
24
|
2
|
|
|
|
|
8
|
return; |
25
|
|
|
|
|
|
|
} elsif ( !$config->{required} && ( !defined( $value ) || $value eq '' ) ) { |
26
|
1
|
|
|
|
|
4
|
return 1; |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
# is it valid? |
30
|
12
|
100
|
|
|
|
65
|
unless ( $value =~ m/^[-+\$]?([0-9,]+(\.[0-9]*)?|\.[0-9]+)$/ ) { # Friedl p128 |
31
|
1
|
|
|
|
|
5
|
$self->error_code( 'dollar_01' ); # not valid |
32
|
1
|
|
|
|
|
5
|
$self->error_message( 'Not a valid dollar value.' ); |
33
|
1
|
|
|
|
|
4
|
return; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
# check parameters |
37
|
|
|
|
|
|
|
# legal ones are min and max |
38
|
11
|
|
|
|
|
14
|
my ( $min, $max ); |
39
|
11
|
|
|
|
|
21
|
$min = $config->{params}{min}; |
40
|
11
|
|
|
|
|
17
|
$max = $config->{params}{max}; |
41
|
|
|
|
|
|
|
|
42
|
11
|
100
|
100
|
|
|
37
|
if ( defined( $min ) && ( $value < $min ) ) { |
43
|
1
|
|
|
|
|
69
|
$self->error_code( 'dollar_02' ); # under min |
44
|
1
|
|
|
|
|
7
|
$self->error_message( "Less than the minimum required (\$$min)." ); |
45
|
1
|
|
|
|
|
4
|
return; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
10
|
100
|
100
|
|
|
30
|
if ( defined( $max ) && ( $value > $max ) ) { |
49
|
1
|
|
|
|
|
4
|
$self->error_code( 'dollar_03' ); # over max |
50
|
1
|
|
|
|
|
5
|
$self->error_message( "More than the maximum allowed (\$$max)." ); |
51
|
1
|
|
|
|
|
4
|
return; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
# send back cleaned up value? |
55
|
|
|
|
|
|
|
# since original value can contain letters etc., you should |
56
|
|
|
|
|
|
|
# never set noclean => 1 |
57
|
9
|
50
|
|
|
|
19
|
unless ( $config->{noclean} ) { |
58
|
9
|
|
|
|
|
22
|
$value =~ tr/-+.0-9//cd; |
59
|
9
|
100
|
|
|
|
114
|
if ($value =~ /^[-+]?\d+$/) { |
60
|
5
|
|
|
|
|
9
|
$value .= ".00"; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
9
|
100
|
|
|
|
30
|
if ($value =~ m/^[-+]?\d+\.\d$/) { |
64
|
1
|
|
|
|
|
3
|
$value .= "0"; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
9
|
|
|
|
|
76
|
my @parts = $value =~ /(^[-+]?\d*\.\d{2})(.*)$/; |
68
|
9
|
100
|
|
|
|
23
|
if ( $parts[1] ) { $value = $1; } |
|
1
|
|
|
|
|
3
|
|
69
|
|
|
|
|
|
|
|
70
|
9
|
|
|
|
|
30
|
$self->value( $value ); |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
9
|
|
|
|
|
95
|
return 1; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
1; |