| 1 | package Mo::utils::Number::Range; | ||
| 2 | |||
| 3 | 4 | ▶ | use base qw(Exporter); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 4 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 4 | ▶ | 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 qw(check_int check_natural check_number check_percent | ||
| 9 | check_positive_decimal check_positive_natural); | ||
| 10 | use Readonly; | ||
| 11 | |||
| 12 | Readonly::Array our @EXPORT_OK => qw(check_int_range check_natural_range | ||
| 13 | check_number_range check_percent_range check_positive_decimal_range | ||
| 14 | check_positive_natural_range); | ||
| 15 | |||
| 16 | our $VERSION = 0.10; | ||
| 17 | |||
| 18 | # ... -2, -1, 0, 1, 2, ... | ||
| 19 | sub check_int_range { | ||
| 20 | my ($self, $key, $min, $max) = @_; | ||
| 21 | |||
| 22 | _check_key($self, $key) && return; | ||
| 23 | |||
| 24 | check_int($self, $key); | ||
| 25 | |||
| 26 | if ($self->{$key} < $min || $self->{$key} > $max) { | ||
| 27 | err "Parameter '".$key."' must be a integer between $min and $max.", | ||
| 28 | 'Value', $self->{$key}, | ||
| 29 | ; | ||
| 30 | } | ||
| 31 | |||
| 32 | return; | ||
| 33 | } | ||
| 34 | |||
| 35 | # 0, 1, 2 ... | ||
| 36 | sub check_natural_range { | ||
| 37 | my ($self, $key, $min, $max) = @_; | ||
| 38 | |||
| 39 | _check_key($self, $key) && return; | ||
| 40 | |||
| 41 | check_natural($self, $key); | ||
| 42 | |||
| 43 | if ($self->{$key} < $min || $self->{$key} > $max) { | ||
| 44 | err "Parameter '".$key."' must be a natural number between $min and $max.", | ||
| 45 | 'Value', $self->{$key}, | ||
| 46 | ; | ||
| 47 | } | ||
| 48 | |||
| 49 | return; | ||
| 50 | } | ||
| 51 | |||
| 52 | # Common number. | ||
| 53 | sub check_number_range { | ||
| 54 | my ($self, $key, $min, $max) = @_; | ||
| 55 | |||
| 56 | _check_key($self, $key) && return; | ||
| 57 | |||
| 58 | check_number($self, $key); | ||
| 59 | |||
| 60 | if ($self->{$key} < $min || $self->{$key} > $max) { | ||
| 61 | err "Parameter '".$key."' must be a number between $min and $max.", | ||
| 62 | 'Value', $self->{$key}, | ||
| 63 | ; | ||
| 64 | } | ||
| 65 | |||
| 66 | return; | ||
| 67 | } | ||
| 68 | |||
| 69 | sub check_percent_range { | ||
| 70 | my ($self, $key, $min, $max) = @_; | ||
| 71 | |||
| 72 | _check_key($self, $key) && return; | ||
| 73 | |||
| 74 | check_percent($self, $key); | ||
| 75 | |||
| 76 | my $value = $self->{$key}; | ||
| 77 | $value =~ s/%$//ms; | ||
| 78 | if ($value < $min || $value > $max) { | ||
| 79 | err "Parameter '".$key."' must be a percent between $min% and $max%.", | ||
| 80 | 'Value', $self->{$key}, | ||
| 81 | ; | ||
| 82 | } | ||
| 83 | |||
| 84 | return; | ||
| 85 | } | ||
| 86 | |||
| 87 | sub check_positive_decimal_range { | ||
| 88 | my ($self, $key, $min, $max) = @_; | ||
| 89 | |||
| 90 | _check_key($self, $key) && return; | ||
| 91 | |||
| 92 | check_positive_decimal($self, $key); | ||
| 93 | |||
| 94 | if ($self->{$key} < $min || $self->{$key} > $max) { | ||
| 95 | err "Parameter '".$key."' must be a positive decimal number between $min and $max.", | ||
| 96 | 'Value', $self->{$key}, | ||
| 97 | ; | ||
| 98 | } | ||
| 99 | |||
| 100 | return; | ||
| 101 | } | ||
| 102 | |||
| 103 | # 1, 2 ... | ||
| 104 | sub check_positive_natural_range { | ||
| 105 | my ($self, $key, $min, $max) = @_; | ||
| 106 | |||
| 107 | _check_key($self, $key) && return; | ||
| 108 | |||
| 109 | check_positive_natural($self, $key); | ||
| 110 | |||
| 111 | if ($self->{$key} < $min || $self->{$key} > $max) { | ||
| 112 | err "Parameter '".$key."' must be a positive natural number between $min and $max.", | ||
| 113 | 'Value', $self->{$key}, | ||
| 114 | ; | ||
| 115 | } | ||
| 116 | |||
| 117 | return; | ||
| 118 | } | ||
| 119 | |||
| 120 | sub _check_key { | ||
| 121 | my ($self, $key) = @_; | ||
| 122 | |||
| 123 | if (! exists $self->{$key} || ! defined $self->{$key}) { | ||
| 124 | return 1; | ||
| 125 | } | ||
| 126 | |||
| 127 | return 0; | ||
| 128 | } | ||
| 129 | |||
| 130 | 1; | ||
| 131 | |||
| 132 | __END__ | ||