File Coverage

blib/lib/Exception/Class/Nested.pm
Criterion Covered Total %
statement 43 43 100.0
branch 12 12 100.0
condition n/a
subroutine 8 8 100.0
pod n/a
total 63 63 100.0


line stmt bran cond sub pod time code
1             package Exception::Class::Nested;
2 3     3   168223 use strict;
  3         9  
  3         112  
3 3     3   17 use warnings;
  3         5  
  3         106  
4 3     3   16 no warnings qw(uninitialized);
  3         10  
  3         127  
5 3     3   19 use Carp;
  3         11  
  3         1445  
6 3     3   3849 use Exception::Class;
  3         41930  
  3         21  
7             our @ISA = qw(Exception::Class);
8            
9             our $VERSION = '0.01';
10            
11             sub import {
12 4     4   786 my $class = shift;
13            
14 4         7 my @classes;
15             my %functions;
16 4         6 while (1) {
17 13         21 my ($name, $opt) = (shift(), shift());
18 13 100       37 last if !defined($name);
19            
20             #print "Root processing ($name, $opt)\n";
21            
22 9 100       30 unless (ref($opt) eq 'HASH') {
23 4         7 unshift(@_,$opt);
24 4         8 $opt = {};
25             }
26 9         39 push @classes, _process_class($name, $opt, undef, \%functions);
27             }
28            
29 4         23 Exception::Class->import(@classes);
30            
31             {
32 3     3   593 no strict 'refs';
  3         15  
  3         706  
  4         19093  
33 4         1819 while (my ($name, $code) = each %functions) {
34 1         2 *{$name} = $code;
  1         2355  
35             #print "Defined \&$name\n";
36             }
37             }
38             }
39            
40             sub _process_class {
41 17     17   28 my ($name, $opt, $parent, $functions) = @_;
42             #print "Processing ($name, $opt)\n";
43 17         17 my @classes;
44 17 100       39 $opt->{isa} = $parent if defined($parent);
45            
46 17         54 while (my ($subname, $subopt) = each %$opt) {
47 27 100       143 next unless ref($subopt);
48 13 100       44 if (ref($subopt) eq 'HASH') {
    100          
49             #print "Found subclass $subname\n";
50 8         35 push @classes, _process_class($subname, $subopt, $name, $functions);
51 8         30 delete $opt->{$subname};
52             } elsif (ref($subopt) eq 'CODE') {
53             #print "Later will define " . $name . '::' . $subname . "\n";
54 1         4 $functions->{$name . '::' . $subname} = $subopt;
55 1         5 delete $opt->{$subname};
56             }
57             }
58 17         59 return ($name, $opt, @classes);
59             }
60            
61             1;
62            
63             =head1 NAME
64            
65             Exception::Class::Nested - Nested declaration of Exception::Class classes
66            
67             =head1 SYNOPSIS
68            
69             use Exception::Class::Nested (
70             'MyException' => {
71             description => 'This is mine!',
72            
73             'YetAnotherException' => {
74             description => 'These exceptions are related to IPC',
75            
76             'ExceptionWithFields' => {
77             fields => [ 'grandiosity', 'quixotic' ],
78             alias => 'throw_fields',
79             full_message => sub {
80             my $self = shift;
81             my $msg = $self->message;
82             $msg .= " and grandiosity was " . $self->grandiosity;
83             return $msg;
84             }
85             }
86             }
87             },
88             );
89            
90             =head1 DESCRIPTION
91            
92             This is little more than a thin wrapper around the C call. It allows you do nest the class
93             declarations instead of having to repeat the class names in the isa=> parameters. It also allows you to
94             define/overload methods in the classes.
95            
96             =head1 SUPPORT
97            
98             Please submit bugs to the CPAN RT system at
99             http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Exception%3A%3AClass%3A%3ANested or
100             via email at bug-exception-class-nested@rt.cpan.org.
101            
102             =head1 AUTHOR
103            
104             Jenda Krynicky,
105            
106             =head1 COPYRIGHT
107            
108             Copyright (c) 2008 Jenda Krynicky. All rights reserved. This
109             program is free software; you can redistribute it and/or modify it
110             under the same terms as Perl itself.
111            
112             The full text of the license can be found in the LICENSE file included
113             with this module.
114            
115             =cut
116