| 1 | package Mo::utils::Number; | ||
| 2 | |||
| 3 | 2 | ▶ | use base qw(Exporter); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | 0 | ▶ | use Error::Pure qw(err); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 8 | use Mo::utils::Number::Utils qw(sub_check_percent); | ||
| 9 | use Readonly; | ||
| 10 | use Scalar::Util qw(looks_like_number); | ||
| 11 | |||
| 12 | Readonly::Array our @EXPORT_OK => qw(check_int check_natural check_number | ||
| 13 | check_percent check_positive_decimal check_positive_natural); | ||
| 14 | |||
| 15 | our $VERSION = 0.10; | ||
| 16 | |||
| 17 | # ... -2, -1, 0, 1, 2, ... | ||
| 18 | sub check_int { | ||
| 19 | my ($self, $key) = @_; | ||
| 20 | |||
| 21 | _check_key($self, $key) && return; | ||
| 22 | |||
| 23 | if ($self->{$key} !~ m/^\-?\d+$/ms) { | ||
| 24 | err "Parameter '$key' must be a integer.", | ||
| 25 | 'Value', $self->{$key}, | ||
| 26 | ; | ||
| 27 | } | ||
| 28 | |||
| 29 | return; | ||
| 30 | } | ||
| 31 | |||
| 32 | # 0, 1, 2 ... | ||
| 33 | sub check_natural { | ||
| 34 | my ($self, $key) = @_; | ||
| 35 | |||
| 36 | _check_key($self, $key) && return; | ||
| 37 | |||
| 38 | if ($self->{$key} !~ m/^\d+$/ms) { | ||
| 39 | err "Parameter '$key' must be a natural number.", | ||
| 40 | 'Value', $self->{$key}, | ||
| 41 | ; | ||
| 42 | } | ||
| 43 | |||
| 44 | return; | ||
| 45 | } | ||
| 46 | |||
| 47 | # Common number. | ||
| 48 | sub check_number { | ||
| 49 | my ($self, $key) = @_; | ||
| 50 | |||
| 51 | _check_key($self, $key) && return; | ||
| 52 | |||
| 53 | if (! looks_like_number($self->{$key})) { | ||
| 54 | err "Parameter '$key' must be a number.", | ||
| 55 | 'Value', $self->{$key}, | ||
| 56 | ; | ||
| 57 | } | ||
| 58 | |||
| 59 | return; | ||
| 60 | } | ||
| 61 | |||
| 62 | sub check_percent { | ||
| 63 | my ($self, $key) = @_; | ||
| 64 | |||
| 65 | _check_key($self, $key) && return; | ||
| 66 | |||
| 67 | sub_check_percent($self->{$key}, $key, 'percent value'); | ||
| 68 | |||
| 69 | return; | ||
| 70 | } | ||
| 71 | |||
| 72 | sub check_positive_decimal { | ||
| 73 | my ($self, $key) = @_; | ||
| 74 | |||
| 75 | _check_key($self, $key) && return; | ||
| 76 | |||
| 77 | if ($self->{$key} !~ m/^\d+(?:\.\d+)?(?:[eE][+-]?\d+)?$/ms || $self->{$key} == 0) { | ||
| 78 | err "Parameter '$key' must be a positive decimal number.", | ||
| 79 | 'Value', $self->{$key}, | ||
| 80 | ; | ||
| 81 | } | ||
| 82 | |||
| 83 | return; | ||
| 84 | } | ||
| 85 | |||
| 86 | # 1, 2 ... | ||
| 87 | sub check_positive_natural { | ||
| 88 | my ($self, $key) = @_; | ||
| 89 | |||
| 90 | _check_key($self, $key) && return; | ||
| 91 | |||
| 92 | if ($self->{$key} !~ m/^\d+$/ms || $self->{$key} == 0) { | ||
| 93 | err "Parameter '$key' must be a positive natural number.", | ||
| 94 | 'Value', $self->{$key}, | ||
| 95 | ; | ||
| 96 | } | ||
| 97 | |||
| 98 | return; | ||
| 99 | } | ||
| 100 | |||
| 101 | sub _check_key { | ||
| 102 | my ($self, $key) = @_; | ||
| 103 | |||
| 104 | if (! exists $self->{$key} || ! defined $self->{$key}) { | ||
| 105 | return 1; | ||
| 106 | } | ||
| 107 | |||
| 108 | return 0; | ||
| 109 | } | ||
| 110 | |||
| 111 | 1; | ||
| 112 | |||
| 113 | __END__ | ||