File Coverage

blib/lib/Eidolon/Core/Exception.pm
Criterion Covered Total %
statement 36 37 97.3
branch 2 6 33.3
condition n/a
subroutine 11 12 91.6
pod 5 5 100.0
total 54 60 90.0


line stmt bran cond sub pod time code
1             package Eidolon::Core::Exception;
2             # ==============================================================================
3             #
4             # Eidolon
5             # Copyright (c) 2009, Atma 7
6             # ---
7             # Eidolon/Core/Exception.pm - base exception class
8             #
9             # ==============================================================================
10              
11 9     9   28114 use base qw/Class::Accessor::Fast/;
  9         16  
  9         7691  
12 9     9   40936 use warnings;
  9         20  
  9         262  
13 9     9   45 use strict;
  9         17  
  9         468  
14              
15             __PACKAGE__->mk_accessors(qw/message line file/);
16              
17             our $VERSION = "0.02"; # 2009-05-14 04:42:38
18              
19 9     9   57 use constant TITLE => "Base exception";
  9         15  
  9         1026  
20              
21             # overload some operations
22             use overload
23 2     2   42 "bool" => sub { 1 },
24 9         91 "eq" => "overloaded_equ",
25 9     9   17794 '""' => "overloaded_str";
  9         10309  
26              
27             # ------------------------------------------------------------------------------
28             # \% new($message)
29             # constructor
30             # ------------------------------------------------------------------------------
31             sub new
32             {
33 2     2 1 3 my ($class, $message, $self);
34              
35 2         5 ($class, $message) = @_;
36              
37             # class attributes
38 2         10 $self =
39             {
40             "message" => $message,
41             "line" => undef,
42             "file" => undef
43             };
44              
45 2         5 bless $self, $class;
46 2         104 $self->_init;
47              
48 2         19 return $self;
49             }
50              
51             # ------------------------------------------------------------------------------
52             # _init()
53             # class initialization
54             # ------------------------------------------------------------------------------
55             sub _init
56             {
57 2     2   4 my ($self, $file, $line);
58              
59 2         4 $self = shift;
60              
61             # get caller info
62 2         14 (undef, $file, $line) = caller(2);
63              
64 2         13 $self->file( $file );
65 2         277 $self->line( $line );
66             }
67              
68             # ------------------------------------------------------------------------------
69             # throw()
70             # throw exception
71             # ------------------------------------------------------------------------------
72             sub throw
73             {
74 2     2 1 1904 my $class = shift;
75              
76 2 50       8 $class->rethrow(@_) if (ref $class);
77 2         11 die $class->new(@_);
78             }
79              
80             # ------------------------------------------------------------------------------
81             # rethrow()
82             # rethrow exception
83             # ------------------------------------------------------------------------------
84             sub rethrow
85             {
86 0 0   0 1 0 die $_[0] if (ref $_[0]);
87             }
88              
89             # ------------------------------------------------------------------------------
90             # $ overloaded_equ($class)
91             # check the type of exception
92             # ------------------------------------------------------------------------------
93             sub overloaded_equ
94             {
95 3     3 1 843 return $_[0]->isa($_[1]);
96             }
97              
98             # ------------------------------------------------------------------------------
99             # $ overloaded_str()
100             # stringify exception
101             # ------------------------------------------------------------------------------
102             sub overloaded_str
103             {
104 1     1 1 1628 my ($self, $str);
105              
106 1         2 $self = shift;
107 1         7 $str = $self->TITLE;
108 1 50       4 $str .= $self->message ? ": ".$self->message : "";
109              
110 1         21 return $str;
111             }
112              
113             1;
114              
115             __END__