NAME
Error::Hierarchy::Util - Assertions and other tools
VERSION
version 1.103530
SYNOPSIS
use Error::Hierarchy::Util qw/assert_defined assert_is_integer/;
sub foo {
my ($self, $bar, $baz) = @_;
assert_defined $bar, 'called without defined bar';
assert_is_integer $baz;
# ...
}
DESCRIPTION
This module provides some functions that can make assertions about a given value and, if necessary, throw an appropriate exception. It also provides other tools.
None of the functions are exported by default, but they can be imported using the standard Exporter semantics.
FUNCTIONS
assert_arrayref($value, $error_message)
assert_arrayref $r, '$r is not an array reference';
Takes as arguments a value and a custom message. If the value is not an array reference, it throws a Error::Hierarchy::Internal::NoArrayRef exception with the given custom message.
assert_nonempty_arrayref($value, $error_message)
assert_nonempty_arrayref $r, '$r does not have any elements';
Takes as arguments a value and a custom message. If the value is not a reference to an array with at least one element, it throws a Error::Hierarchy::Internal::EmptyArrayRef exception with the given custom message.
assert_hashref($value, $error_message)
assert_hashref $r, '$r is not a hash reference';
Takes as arguments a value and a custom message. If the value is not a hash reference, it throws a Error::Hierarchy::Internal::NoHashRef exception with the given custom message.
assert_nonempty_hashref($value, $error_message)
assert_nonempty_hashref $r, '$r does not have any key/value pairs';
Takes as arguments a value and a custom message. If the value is not a reference to a hash with at least one key/value pair, it throws a Error::Hierarchy::Internal::EmptyHashRef exception with the given custom message.
assert_class($object, $class_name)
assert_class $obj, 'Some::Class';
Takes as arguments an object and a class name. If the object is not of the given class type, it throws a Error::Hierarchy::Internal::Class exception.
assert_defined($expression, $error_message)
sub foo {
my ($self, $bar) = @_;
assert_defined $bar, 'called without bar';
...
}
Takes as arguments a value and a custom message. If the value is not defined, it throws a Error::Hierarchy::Internal::ValueUndefined exception with the given custom message.
assert_read_only(@args)
sub get_foo {
my $self = shift;
assert_read_only(@_);
$self->{foo};
}
Checks whether the calling subroutine was called with any arguments. If so, it throws a Error::Hierarchy::Internal::ReadOnlyAttribute exception.
assert_is_integer($value)
sub set_log_level {
my ($self, $log_level) = @_;
assert_is_integer($log_level);
...
}
Takes a value and unless it is an integer between 1 and 9, it throws a Error::Hierarchy::Internal::CustomMessage exception with a predefined message.
The limitation of the value makes this probably a function that's not very useful. I should make this more generic.
assert_condition($value, $exception_class, $error_message))
This function is used internally by many other assert_*()
functions.
Checks whether the condition indicated by the value is true. If so, it just returns. If the condition is false, an exception of the indicated class is thrown, with the given message. The call stack is checked to find the first caller whose name does not begin with assert_
. That caller's name is prepended to the custom message.
The exception class indicated by $exception_class
is supposed to be a marker subclass of Error::Hierarchy::Internal::CustomMessage.
assert_getopt()
Provides integration for Data::Conveyor
. In command-line applications, this function can be called to verify options passed to it. If the value given is true, we just return. If it is false, we throw a special "help exception".
This should be moved to Data::Conveyor
.
assert_named_args(\%args, @args_spec)
This function helps in validating named arguments passed to a method or function. The first argument is a reference to the hash of named arguments, the remaining arguments define which argument names are allowed. If an argument name starts with a +
sign, it means that this argument is mandatory.
For example, assume your method only accepts the keys foo
, bar
and baz
, and bar
is mandatory. Usually you will write your method like this:
sub my_method {
my ($self, %args) = @_;
assert_named_args(\%args, qw(foo +bar baz));
# ...
}
If there was a validation error, an exception of type Error::Hierarchy::Internal::CustomMessage will be thrown with details about what went wrong.
assert_enum()
Takes a value and a reference to an array of valid values - that is, the enumeration. If the value is not among the enumerated valid values, an exception is thrown.
loader_callback($coderef)
Support for "virtual" classes that do not exist as files. It is used in load_class()
, so see its documentation.
load_class($class, $verbose))
load_class 'Some::Class', 1;
Takes as arguments a package name and a boolean verbosity flag. Tries to load the package and if it can't be loaded, it throws a Error::Hierarchy::Internal::CustomMessage exception with the error message obtained when trying to load the package.
To save time, this function checks whether the package defines a $VERSION
and if so, it assumes that the package has already been loaded and returns right away.
If the class can't be loaded via require()
but we have a loader_callback()
, it is invoked with the class name.
If the verbose flag is set, the error code - $@
- is printed immediately if a problem occurs. You might want to set this flag in when testing your code to get a quick feedback on loading problems, but you should have a graceful method to deal with the problem anyway.
This function is called load_class()
and not load_package()
for historical reasons.
INSTALLATION
See perlmodinstall for information and options on installing Perl modules.
BUGS AND LIMITATIONS
No bugs have been reported.
Please report any bugs or feature requests through the web interface at http://rt.cpan.org/Public/Dist/Display.html?Name=Error-Hierarchy.
AVAILABILITY
The latest version of this module is available from the Comprehensive Perl Archive Network (CPAN). Visit http://www.perl.com/CPAN/ to find a CPAN site near you, or see http://search.cpan.org/dist/Error-Hierarchy/.
The development version lives at http://github.com/hanekomu/Error-Hierarchy and may be cloned from git://github.com/hanekomu/Error-Hierarchy. Instead of sending patches, please fork this project using the standard git and github infrastructure.
AUTHOR
Marcel Gruenauer <marcel@cpan.org>
COPYRIGHT AND LICENSE
This software is copyright (c) 2004 by Marcel Gruenauer.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.