File Coverage

blib/lib/Mo/utils/Number/Utils.pm
Criterion Covered Total %
statement 29 29 100.0
branch 10 10 100.0
condition n/a
subroutine 6 6 100.0
pod 1 1 100.0
total 46 46 100.0


line stmt bran cond sub pod time code
1             package Mo::utils::Number::Utils;
2              
3 29     29   172628 use base qw(Exporter);
  29         54  
  29         3713  
4 29     29   199 use strict;
  29         69  
  29         875  
5 29     29   377 use warnings;
  29         86  
  29         1824  
6              
7 29     29   1640 use Error::Pure qw(err);
  29         24474  
  29         1783  
8 29     29   387 use Readonly;
  29         57  
  29         11394  
9              
10             Readonly::Array our @EXPORT_OK => qw(sub_check_percent);
11              
12             our $VERSION = 0.10;
13              
14             sub sub_check_percent {
15 25     25 1 439279 my ($value, $key, $func, $error_value) = @_;
16              
17 25 100       245 if (! defined $error_value) {
18 24         56 $error_value = $value;
19             }
20              
21 25 100       78 if (! defined $func) {
22 10         27 $func = 'percent value';
23             }
24              
25             # Check percent sign.
26 25 100       202 if ($value =~ m/^(\d+(?:\.\d+)?)(\%)?$/ms) {
27 17         60 $value = $1;
28 17         94 my $p = $2;
29 17 100       52 if (! $p) {
30 5         30 err "Parameter '$key' has bad $func (missing %).",
31             'Value', $error_value,
32             ;
33             }
34             # Check percent number.
35             } else {
36 8         51 err "Parameter '$key' has bad $func.",
37             'Value', $error_value,
38             ;
39             }
40              
41             # Check percent value.
42 12 100       51 if ($value > 100) {
43 3         21 err "Parameter '$key' has bad $func.",
44             'Value', $error_value,
45             ;
46             }
47              
48 9         28 return;
49             }
50              
51             1;
52              
53             __END__
54              
55             =pod
56              
57             =encoding utf8
58              
59             =head1 NAME
60              
61             Mo::utils::Number::Utils - Utilities for Mo::utils::Number.
62              
63             =head1 SYNOPSIS
64              
65             use Mo::utils::Number::Utils qw(sub_check_percent);
66              
67             sub_check_percent($value, $key, $func, $error_value);
68              
69             =head1 SUBROUTINES
70              
71             =head2 C<sub_check_percent>
72              
73             sub_check_percent($value, $key, $func, $error_value);
74              
75             Common subroutine for check percents.
76             It is exportable.
77              
78             Returns undef.
79              
80             =head1 ERRORS
81              
82             sub_check_percent():
83              
84             =head1 EXAMPLE
85              
86             =for comment filename=sub_check_percent.pl
87              
88             use strict;
89             use warnings;
90              
91             use Mo::utils::Number::Utils qw(sub_check_percent);
92              
93             my $ret = sub_check_percent('20%', 'key', 'percent value', 'user value');
94             if (! defined $ret) {
95             print "Returns undef.\n";
96             }
97              
98             # Output:
99             # Returns undef.
100              
101             =head1 DEPENDENCIES
102              
103             L<Error::Pure>,
104             L<Exporter>,
105             L<Readonly>.
106              
107             =head1 SEE ALSO
108              
109             =over
110              
111             =item L<Mo::utils::Number>
112              
113             Mo number utilities.
114              
115             =back
116              
117             =head1 REPOSITORY
118              
119             L<https://github.com/michal-josef-spacek/Mo-utils-Number>
120              
121             =head1 AUTHOR
122              
123             Michal Josef Špaček L<mailto:skim@cpan.org>
124              
125             L<http://skim.cz>
126              
127             =head1 LICENSE AND COPYRIGHT
128              
129             © 2024-2026 Michal Josef Špaček
130              
131             BSD 2-Clause License
132              
133             =head1 VERSION
134              
135             0.10
136              
137             =cut