Mo::utils - Mo utilities.
use Mo::utils qw(check_angle check_array check_array_object check_array_required check_bool check_code check_isa check_length check_number check_number_of_items check_regexp check_required check_string_begin check_strings); check_angle($self, $key); check_array($self, $key); check_array_object($self, $key, $class, $class_name); check_array_required($self, $key); check_bool($self, $key); check_code($self, $key); check_isa($self, $key, $class); check_length($self, $key, $max_length); check_number($self, $key); check_number_of_items($self, $list_method, $item_method, $object_name, $item_name); check_regexp($self, $key, $regexp); check_required($self, $key); check_string_begin($self, $key, $string_base); check_strings($self, $key, $strings_ar);
Mo utilities for checking of data objects.
check_angle
check_angle($self, $key);
Check parameter defined by $key which is number between 0 and 360.
$key
Put error if check isn't ok.
Returns undef.
check_array
check_array($self, $key);
Check parameter defined by $key which is reference to array.
check_array_object
check_array_object($self, $key, $class, $class_name);
Check parameter defined by $key which is reference to array with instances of some object type ($class). $class_name is used to error message.
$class
$class_name
check_array_required
check_array_required($self, $key);
Check parameter defined by $key which is reference to array for at least one value inside.
check_bool
check_bool($self, $key);
Check parameter defined by $key if value is bool or not.
check_code
check_code($self, $key);
Check parameter defined by $key which is code reference or no.
check_isa
check_isa($self, $key, $class);
Check parameter defined by $key which is instance of $class or no.
check_length
check_length($self, $key, $max_length);
Check length of value for parameter defined by $key. Maximum length is defined by $max_length.
$max_length
check_number
check_number($self, $key);
Check parameter defined by $key which is number (positive or negative) or no.
check_number_of_items
check_number_of_items($self, $list_method, $item_method, $object_name, $item_name);
Check amount of unique items defined by $item_method method value. List items via $list_method and get value via $item_method method. $object_name and $item_name are variables for error output.
$item_method
$list_method
$object_name
$item_name
check_regexp
check_regexp($self, $key, $regexp);
Check parameter defined by $key via regular expression defined by c<$regexp>.
check_required
check_required($self, $key);
Check required parameter defined by $key.
check_string_begin
check_string_begin($self, $key, $string_base);
Check parameter if it is correct string which begins with base.
Put error if string base doesn't exist. Put error string base isn't present in string on begin.
check_strings
check_strings($self, $key, $strings_ar);
Check parameter if it is correct string from strings list.
Put error if strings definition is undef or not list of strings. Put error if check isn't ok.
check_angle(): From check_number(): Parameter '%s' must be a number. Value: %s Parameter '%s' must be a number between 0 and 360. Value: %s check_array(): Parameter '%s' must be a array. Value: %s Reference: %s check_array_object(): Parameter '%s' must be a array. Value: %s Reference: %s %s isn't '%s' object. check_array_required(): Parameter '%s' is required. Parameter '%s' must be a array. Value: %s Reference: %s Parameter '%s' with array must have at least one item. check_bool(): Parameter '%s' must be a bool (0/1). Value: %s check_code(): Parameter '%s' must be a code. Value: %s check_isa(): Parameter '%s' must be a '%s' object. Value: %s Reference: %s check_length(): Parameter '%s' has length greater than '%s'. Value: %s check_number(): Parameter '%s' must be a number. Value: %s check_number_of_items(): %s for %s '%s' has multiple values. check_regexp(): Parameter '%s' must have defined regexp. Parameter '%s' does not match the specified regular expression. String: %s Regexp: %s check_required(): Parameter '%s' is required. check_string_begin(): Parameter '%s' must have defined string base. Parameter '%s' must begin with defined string base. String: %s String base: %s check_strings(): Parameter '%s' must have strings definition. Parameter '%s' must have right string definition. Parameter '%s' must be one of defined strings. String: %s Possible strings: %s
use strict; use warnings; use Mo::utils qw(check_angle); my $self = { 'key' => 10.1, }; check_angle($self, 'key'); # Print out. print "ok\n"; # Output: # ok
use strict; use warnings; use Error::Pure; use Mo::utils qw(check_angle); $Error::Pure::TYPE = 'Error'; my $self = { 'key' => 400, }; check_angle($self, 'key'); # Print out. print "ok\n"; # Output like: # #Error [..utils.pm:?] Parameter 'key' must be a number between 0 and 360.
use strict; use warnings; use Mo::utils qw(check_array); my $self = { 'key' => ['foo'], }; check_array($self, 'key'); # Print out. print "ok\n"; # Output: # ok
use strict; use warnings; use Error::Pure; use Mo::utils qw(check_array); $Error::Pure::TYPE = 'Error'; my $self = { 'key' => 'foo', }; check_array($self, 'key'); # Print out. print "ok\n"; # Output like: # #Error [..utils.pm:?] Parameter 'key' must be a array.
use strict; use warnings; use Mo::utils qw(check_array_object); use Test::MockObject; my $self = { 'key' => [ Test::MockObject->new, ], }; check_array_object($self, 'key', 'Test::MockObject', 'Value'); # Print out. print "ok\n"; # Output: # ok
use strict; use warnings; use Error::Pure; use Mo::utils qw(check_array_object); $Error::Pure::TYPE = 'Error'; my $self = { 'key' => [ 'foo', ], }; check_array_object($self, 'key', 'Test::MockObject', 'Value'); # Print out. print "ok\n"; # Output like: # #Error [..utils.pm:?] Value isn't 'Test::MockObject' object.
use strict; use warnings; use Mo::utils qw(check_array_required); my $self = { 'key' => ['value'], }; check_array_required($self, 'key'); # Print out. print "ok\n"; # Output: # ok
use strict; use warnings; use Error::Pure; use Mo::utils qw(check_array_required); $Error::Pure::TYPE = 'Error'; my $self = { 'key' => [], }; check_array_required($self, 'key'); # Print out. print "ok\n"; # Output like: # #Error [..utils.pm:?] Parameter 'key' with array must have at least one item.
use strict; use warnings; use Mo::utils qw(check_bool); use Test::MockObject; my $self = { 'key' => 1, }; check_bool($self, 'key'); # Print out. print "ok\n"; # Output: # ok
use strict; use warnings; use Error::Pure; use Mo::utils qw(check_bool); $Error::Pure::TYPE = 'Error'; my $self = { 'key' => 'bad', }; check_bool($self, 'key'); # Print out. print "ok\n"; # Output like: # #Error [..utils.pm:?] Parameter 'key' must be a bool (0/1).
use strict; use warnings; use Mo::utils qw(check_code); use Test::MockObject; my $self = { 'key' => sub {}, }; check_code($self, 'key'); # Print out. print "ok\n"; # Output: # ok
use strict; use warnings; use Error::Pure; use Mo::utils qw(check_code); $Error::Pure::TYPE = 'Error'; my $self = { 'key' => 'bad', }; check_code($self, 'key'); # Print out. print "ok\n"; # Output like: # #Error [..utils.pm:?] Parameter 'key' must be a code.
use strict; use warnings; use Mo::utils qw(check_isa); use Test::MockObject; my $self = { 'key' => Test::MockObject->new, }; check_isa($self, 'key', 'Test::MockObject'); # Print out. print "ok\n"; # Output: # ok
use strict; use warnings; $Error::Pure::TYPE = 'Error'; use Mo::utils qw(check_isa); my $self = { 'key' => 'foo', }; check_isa($self, 'key', 'Test::MockObject'); # Print out. print "ok\n"; # Output like: # #Error [...utils.pm:?] Parameter 'key' must be a 'Test::MockObject' object.
use strict; use warnings; $Error::Pure::TYPE = 'Error'; use Mo::utils qw(check_length); my $self = { 'key' => 'foo', }; check_length($self, 'key', 3); # Print out. print "ok\n"; # Output like: # ok
use strict; use warnings; $Error::Pure::TYPE = 'Error'; use Mo::utils qw(check_length); my $self = { 'key' => 'foo', }; check_length($self, 'key', 2); # Print out. print "ok\n"; # Output like: # #Error [...utils.pm:?] Parameter 'key' has length greater than '2'.
use strict; use warnings; use Mo::utils qw(check_number); my $self = { 'key' => '10', }; check_number($self, 'key'); # Print out. print "ok\n"; # Output: # ok
use strict; use warnings; $Error::Pure::TYPE = 'Error'; use Mo::utils qw(check_number); my $self = { 'key' => 'foo', }; check_number($self, 'key'); # Print out. print "ok\n"; # Output like: # #Error [...utils.pm:?] Parameter 'key' must be a number.
use strict; use warnings; use Test::MockObject; $Error::Pure::TYPE = 'Error'; use Mo::utils qw(check_number_of_items); # Item object #1. my $item1 = Test::MockObject->new; $item1->mock('value', sub { return 'value1', }); # Item object #1. my $item2 = Test::MockObject->new; $item2->mock('value', sub { return 'value2', }); # Tested object. my $self = Test::MockObject->new({ 'key' => [], }); $self->mock('list', sub { return [ $item1, $item2, ]; }); # Check number of items. check_number_of_items($self, 'list', 'value', 'Test', 'Item'); # Print out. print "ok\n"; # Output like: # ok
use strict; use warnings; use Test::MockObject; $Error::Pure::TYPE = 'Error'; use Mo::utils qw(check_number_of_items); # Item object #1. my $item1 = Test::MockObject->new; $item1->mock('value', sub { return 'value1', }); # Item object #2. my $item2 = Test::MockObject->new; $item2->mock('value', sub { return 'value1', }); # Tested object. my $self = Test::MockObject->new({ 'key' => [], }); $self->mock('list', sub { return [ $item1, $item2, ]; }); # Check number of items. check_number_of_items($self, 'list', 'value', 'Test', 'Item'); # Print out. print "ok\n"; # Output like: # #Error [...utils.pm:?] Test for Item 'value1' has multiple values.
use strict; use warnings; use Mo::utils qw(check_regexp); my $self = { 'key' => 'https://example.com/1', }; check_regexp($self, 'key', qr{^https://example\.com/\d+$}); # Print out. print "ok\n"; # Output: # ok
use strict; use warnings; use Error::Pure; use Mo::utils qw(check_regexp); $Error::Pure::TYPE = 'Error'; my $self = { 'key' => 'https://example.com/bad', }; check_regexp($self, 'key', qr{^https://example\.com/\d+$}); # Print out. print "ok\n"; # Output like: # #Error [...utils.pm:?] Parameter 'key' does not match the specified regular expression.
use strict; use warnings; use Mo::utils qw(check_required); my $self = { 'key' => 'value', }; check_required($self, 'key'); # Print out. print "ok\n"; # Output: # ok
use strict; use warnings; use Error::Pure; use Mo::utils qw(check_required); $Error::Pure::TYPE = 'Error'; my $self = { 'key' => undef, }; check_required($self, 'key'); # Print out. print "ok\n"; # Output like: # #Error [...utils.pm:?] Parameter 'key' is required.
use strict; use warnings; use Mo::utils qw(check_string_begin); my $self = { 'key' => 'http://example.com/foo', }; check_string_begin($self, 'key', 'http://example.com/'); # Print out. print "ok\n"; # Output: # ok
use strict; use warnings; use Error::Pure; use Mo::utils qw(check_string_begin); $Error::Pure::TYPE = 'Error'; my $self = { 'key' => 'http://example/foo', }; check_string_begin($self, 'key', 'http://example.com/'); # Print out. print "ok\n"; # Output like: # #Error [...utils.pm:?] Parameter 'key' must begin with defined string base.
use strict; use warnings; use Mo::utils qw(check_strings); my $self = { 'key' => 'value', }; check_strings($self, 'key', ['value', 'foo']); # Print out. print "ok\n"; # Output: # ok
use strict; use warnings; use Error::Pure; use Mo::utils qw(check_strings); $Error::Pure::TYPE = 'Error'; my $self = { 'key' => 'bar', }; check_strings($self, 'key', ['foo', 'value']); # Print out. print "ok\n"; # Output like: # #Error [...utils.pm:?] Parameter 'key' must be one of defined strings.
Exporter, Error::Pure, List::Utils, Readonly, Scalar::Util.
Micro Objects. Mo is less.
Mo language utilities.
Wikibase datatype utilities.
https://github.com/michal-josef-spacek/Mo-utils
Michal Josef Špaček mailto:skim@cpan.org
http://skim.cz
© 2020-2023 Michal Josef Špaček
BSD 2-Clause License
0.20
To install Mo::utils, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mo::utils
CPAN shell
perl -MCPAN -e shell install Mo::utils
For more information on module installation, please visit the detailed CPAN module installation guide.