| 1 | package Exception::Class::Nested; | ||||||||||||||
| 2 | 3 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 3 | ▶ | no warnings qw(uninitialized); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 3 | ▶ | use Carp; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 3 | ▶ | use Exception::Class; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 3 | our @ISA = qw(Exception::Class); | |||||||||||||
| 8 | |||||||||||||||
| 9 | 3 | our $VERSION = '0.01'; | |||||||||||||
| 10 | |||||||||||||||
| 11 | 4 | ▶ | sub import { | ||||||||||||
|
Subroutine
import: called CC=4 SCAR=13.9
| |||||||||||||||
| 12 | 4 | my $class = shift; | |||||||||||||
| 13 | |||||||||||||||
| 14 | 4 | my @classes; | |||||||||||||
| 15 | my %functions; | ||||||||||||||
| 16 | 4 | while (1) { | |||||||||||||
| 17 | 13 | my ($name, $opt) = (shift(), shift()); | |||||||||||||
| 18 | 13 | ▶ | last if !defined($name); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 19 | |||||||||||||||
| 20 | #print "Root processing ($name, $opt)\n"; | ||||||||||||||
| 21 | |||||||||||||||
| 22 | 9 | ▶ | unless (ref($opt) eq 'HASH') { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 23 | 4 | unshift(@_,$opt); | |||||||||||||
| 24 | 4 | $opt = {}; | |||||||||||||
| 25 | } | ||||||||||||||
| 26 | 9 | push @classes, _process_class($name, $opt, undef, \%functions); | |||||||||||||
| 27 | } | ||||||||||||||
| 28 | |||||||||||||||
| 29 | 4 | Exception::Class->import(@classes); | |||||||||||||
| 30 | |||||||||||||||
| 31 | { | ||||||||||||||
| 32 | 3 | ▶ | no strict 'refs'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 33 | 4 | ▶ | while (my ($name, $code) = each %functions) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 34 | 1 | *{$name} = $code; | |||||||||||||
| 35 | #print "Defined \&$name\n"; | ||||||||||||||
| 36 | } | ||||||||||||||
| 37 | } | ||||||||||||||
| 38 | } | ||||||||||||||
| 39 | |||||||||||||||
| 40 | 17 | ▶ | sub _process_class { | ||||||||||||
|
Subroutine
_process_class: called CC=6 SCAR=17.9
| |||||||||||||||
| 41 | 17 | my ($name, $opt, $parent, $functions) = @_; | |||||||||||||
| 42 | #print "Processing ($name, $opt)\n"; | ||||||||||||||
| 43 | 17 | my @classes; | |||||||||||||
| 44 | 17 | ▶ | $opt->{isa} = $parent if defined($parent); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 45 | |||||||||||||||
| 46 | 17 | ▶ | while (my ($subname, $subopt) = each %$opt) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 47 | 27 | ▶ | next unless ref($subopt); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 48 | 13 | ▶ | if (ref($subopt) eq 'HASH') { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 49 | #print "Found subclass $subname\n"; | ||||||||||||||
| 50 | 8 | push @classes, _process_class($subname, $subopt, $name, $functions); | |||||||||||||
| 51 | 8 | delete $opt->{$subname}; | |||||||||||||
| 52 | } elsif (ref($subopt) eq 'CODE') { | ||||||||||||||
| 53 | #print "Later will define " . $name . '::' . $subname . "\n"; | ||||||||||||||
| 54 | 1 | $functions->{$name . '::' . $subname} = $subopt; | |||||||||||||
| 55 | 1 | delete $opt->{$subname}; | |||||||||||||
| 56 | } | ||||||||||||||
| 57 | } | ||||||||||||||
| 58 | 17 | return ($name, $opt, @classes); | |||||||||||||
| 59 | } | ||||||||||||||
| 60 | |||||||||||||||
| 61 | 3 | 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<use Exception::Class> 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, <jenda@krynicky.cz> | ||||||||||||||
| 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 | |||||||||||||||