Eidolon::Core::Exception::Builder - exception builder for Eidolon.


Eidolon documentation Contained in the Eidolon distribution.

Index


Code Index:

NAME

Top

Eidolon::Core::Exception::Builder - exception builder for Eidolon.

SYNOPSIS

Top

In one of your application files, for example lib/Example/Exceptions.pm you can write:

    use Eidolon::Core::Exception::Builder
    (
        "MyException" =>
        {
            "title" => "Something happened."
        },

        "MyException::Terrible" =>
        {
            "isa"   => "MyException",
            "title" => "Something terrble happened."
        },

        "MyException::Good" =>
        {
            "isa"   => "MyException",
            "title" => "Something good happened."
        }
    );

DESCRIPTION

Top

The Eidolon::Core::Exception::Builder class provides an easy way to create own exceptions. It has no methods that you should call - all work is done during package import.

METHODS

Top

import($class, $data)

Creates exception $class with $data settings. $data is a hashref, containing inheritance information and exception message:

* isa

Exception base class.

* title

Exception message.

If no isa information is specified, Eidolon::Core::Exception will be used as a base exception class.

SEE ALSO

Top

Eidolon, Eidolon::Core::Exception

LICENSE

Top

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

AUTHOR

Top

Anton Belousov, <abel@cpan.org>

COPYRIGHT

Top


Eidolon documentation Contained in the Eidolon distribution.

package Eidolon::Core::Exception::Builder;
# ==============================================================================
#
#   Eidolon
#   Copyright (c) 2009, Atma 7
#   ---
#   Eidolon/Core/Exception/Builder.pm - exception builder
#
# ==============================================================================

use warnings;
use strict;

our $VERSION = "0.02"; # 2009-05-12 06:24:56

# ------------------------------------------------------------------------------
# import($class, $data)
# create exceptions classes
# ------------------------------------------------------------------------------
sub import
{
    my ($self, $class, $data, $isa, $title, $code);

    $self = shift;

    while ($class = shift) 
    {
        $data  = ref $_[0] ? shift : {};
        $isa   = $data->{"isa"} || "Eidolon::Core::Exception";
        $title = $data->{"title"};

        # check if base class exists
        {
            no strict "refs";
            die "Base class doesn't exist: $isa" if (!keys %{"$isa\::"});
        }

        $code  = "package $class;\nuse base qw/$isa/;\n";
        $code .= "use constant TITLE => '$title';\n" if ($title);

        eval $code;
    }
}

1;

__END__