The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Error::Hierarchy::Util - assertions and other tools

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
    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
    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
    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
    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
    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
    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
    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
    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.

FIXME: The limitation of the value makes this probably a function that's not very useful. I should make this more generic.

load_class
    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 laoded, 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 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.

TAGS

If you talk about this module in blogs, on del.icio.us or anywhere else, please use the errorhierarchy tag.

BUGS AND LIMITATIONS

No bugs have been reported.

Please report any bugs or feature requests to bug-error-hierarchy@rt.cpan.org, or through the web interface at http://rt.cpan.org.

INSTALLATION

See perlmodinstall for information and options on installing Perl modules.

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://www.perl.com/CPAN/authors/id/M/MA/MARCEL/>.

AUTHOR

Marcel Grünauer, <marcel@cpan.org>

COPYRIGHT AND LICENSE

Copyright 2007 by Marcel Grünauer

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.