use Exception::Class::Nested ( 'MyException' => { description => 'This is mine!', 'YetAnotherException' => { description => 'These exceptions are related to IPC', 'ExceptionWithFields' => { fields => [ 'grandiosity', 'quixotic' ], alias => 'throw_fields', full_message => sub { my $self = shift; my $msg = $self->message; $msg .= " and grandiosity was " . $self->grandiosity; return $msg; } } } } );

Especially if you create a bigger hierarchy of Exception::Class classes it may get annoying to have to repeat the names of the classes in the isa=> parameters, not speaking about the fact that a typo in such a parameter may cause hard to debug errors.

I think the nested declaration is clearer and safer .. though it assumes that no Exception::Class parameter is gonna be a hashref.

Apart from this the module simplifies the declaration or overloading of methods in the classes.

I've just uploaded the module to CPAN, you can also find it here.

package Exception::Class::Nested; use strict; use warnings; no warnings qw(uninitialized); use Carp; use Exception::Class; our @ISA = qw(Exception::Class); our $VERSION = '0.01'; sub import { my $class = shift; my @classes; my %functions; while (1) { my ($name, $opt) = (shift(), shift()); last if !defined($name); #print "Root processing ($name, $opt)\n"; unless (ref($opt) eq 'HASH') { unshift(@_,$opt); $opt = {}; } push @classes, _process_class($name, $opt, undef, \%functions) +; } Exception::Class->import(@classes); { no strict 'refs'; while (my ($name, $code) = each %functions) { *{$name} = $code; #print "Defined \&$name\n"; } } } sub _process_class { my ($name, $opt, $parent, $functions) = @_; #print "Processing ($name, $opt)\n"; my @classes; $opt->{isa} = $parent if defined($parent); while (my ($subname, $subopt) = each %$opt) { next unless ref($subopt); if (ref($subopt) eq 'HASH') { #print "Found subclass $subname\n"; push @classes, _process_class($subname, $subopt, $name, $f +unctions); delete $opt->{$subname}; } elsif (ref($subopt) eq 'CODE') { #print "Later will define " . $name . '::' . $subname . "\n"; $functions->{$name . '::' . $subname} = $subopt; delete $opt->{$subname}; } } return ($name, $opt, @classes); } 1; =head1 NAME Exception::Class::Nested - Nested declaration of Exception::Class clas +ses =head1 SYNOPSIS use Exception::Class::Nested ( 'MyException' => { description => 'This is mine!', 'YetAnotherException' => { description => 'These exceptions are related to IPC', 'ExceptionWithFields' => { fields => [ 'grandiosity', 'quixotic' ], alias => 'throw_fields', full_message => sub { my $self = shift; my $msg = $self->message; $msg .= " and grandiosity was " . $self->grand +iosity; return $msg; } } } }, ); =head1 DESCRIPTION This is little more than a thin wrapper around the C<use Exception::Cl +ass> call. It allows you to nest the class declarations instead of having to repeat the class names in the isa=> +parameters. It also allows you to define/overload methods in the classes. =head1 SUPPORT Please submit bugs to the CPAN RT system at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Exception%3A%3AClass%3A +%3ANested or via email at bug-exception-class-nested@rt.cpan.org. =head1 AUTHOR Jenda Krynicky, <jenda@krynicky.cz> =head1 COPYRIGHT Copyright (c) 2008 Jenda Krynicky. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. The full text of the license can be found in the LICENSE file included with this module. =cut

In reply to Exception::Class::Nested, nest the hierarchy by Jenda

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.