Subject: | [PATCH] Error::Exception - Combined Interface Example |
After reading Error and Exception::Class, I really wanted to be able to
do this:
use Error::Exception (
MyError => { description => 'A description', },
);
try {
MyError->throw( 'an error' );
} catch MyError with {
my $e = shift;
warn "Caught: ".$e->description;
} finally {
warn "Cleanup";
};
This patch provides that functionality. I will write more tests and
update docs if this is of value.
Subject: | Error-Exception-1.0_new-interface.patch |
diff -ruN Error-Exception-1.0/examples/error-exception.pl Error-Exception-1.1/examples/error-exception.pl
--- Error-Exception-1.0/examples/error-exception.pl 1969-12-31 16:00:00.000000000 -0800
+++ Error-Exception-1.1/examples/error-exception.pl 2008-09-03 01:28:07.000000000 -0700
@@ -0,0 +1,14 @@
+#!/usr/bin/perl
+
+use Error::Exception (
+ MyError => { description => 'A description', },
+);
+
+try {
+ MyError->throw( 'an error' );
+} catch MyError with {
+ my $e = shift;
+ warn "Caught: ".$e->description;
+} finally {
+ warn "Cleanup";
+};
diff -ruN Error-Exception-1.0/lib/Error/Exception.pm Error-Exception-1.1/lib/Error/Exception.pm
--- Error-Exception-1.0/lib/Error/Exception.pm 2008-06-27 08:22:50.000000000 -0700
+++ Error-Exception-1.1/lib/Error/Exception.pm 2008-09-03 00:20:43.000000000 -0700
@@ -21,10 +21,19 @@
use strict;
use warnings;
+our $VERSION = '1.1';
+BEGIN { $Exception::Class::BASE_EXC_CLASS = 'Error'; }
+
use Exception::Class;
-use base qw( Error Exception::Class::Base );
+*import = \&Exception::Class::import;
+*_make_subclass = \&Exception::Class::_make_subclass;
+
+use base qw( Exporter Error Exception::Class::Base );
+
+use Error qw/:try/;
+our @EXPORT = qw(try with finally except otherwise);
+__PACKAGE__->export_to_level(1, ( __PACKAGE__, @EXPORT) );
-our $VERSION = '1.0';
# This "no critic" is open because I couldn't find the policy name
sub new { ## no critic
diff -ruN Error-Exception-1.0/t/10-load-try-syntax.t Error-Exception-1.1/t/10-load-try-syntax.t
--- Error-Exception-1.0/t/10-load-try-syntax.t 1969-12-31 16:00:00.000000000 -0800
+++ Error-Exception-1.1/t/10-load-try-syntax.t 2008-09-03 01:14:45.000000000 -0700
@@ -0,0 +1,19 @@
+
+use strict;
+use warnings;
+
+use Test::More qw/no_plan/;
+
+BEGIN { use_ok( 'Error::Exception' ); }
+
+can_ok( __PACKAGE__, qw/ try with finally except otherwise / );
+
+# Here's where'd I'd also like to test if we can 'throw'. But then I realize
+# that Error's 'throw' is actually an object constructor and not a procedure.
+# I'm apt to say that that should be corrected so that it "does what I mean"
+# in the right context:
+# throw 'An error';
+# Rather than needing:
+# throw Error 'An error';
+# But of course you can just use 'die':
+# die 'An error';
diff -ruN Error-Exception-1.0/t/10-try-catch.t Error-Exception-1.1/t/10-try-catch.t
--- Error-Exception-1.0/t/10-try-catch.t 1969-12-31 16:00:00.000000000 -0800
+++ Error-Exception-1.1/t/10-try-catch.t 2008-09-03 01:20:00.000000000 -0700
@@ -0,0 +1,17 @@
+
+use strict;
+use warnings;
+
+use Test::More qw/ no_plan /;
+use Test::Exception;
+
+BEGIN { use_ok( 'Error::Exception' ); }
+
+lives_ok {
+ try { throw Error 'An error' } catch Error with { };
+} 'Caught simple error.';
+
+dies_ok {
+ try { throw Error 'An error' } catch NoCatch with { };
+} 'Simple uncaught exception.';
+
diff -ruN Error-Exception-1.0/t/20-load-exception-classes.t Error-Exception-1.1/t/20-load-exception-classes.t
--- Error-Exception-1.0/t/20-load-exception-classes.t 1969-12-31 16:00:00.000000000 -0800
+++ Error-Exception-1.1/t/20-load-exception-classes.t 2008-09-03 01:23:42.000000000 -0700
@@ -0,0 +1,10 @@
+
+use strict;
+use warnings;
+
+use Test::More qw/no_plan/;
+use Test::Exception;
+
+BEGIN { use_ok( 'Error::Exception', qw/ MyError / ); }
+
+can_ok( 'MyError', qw/throw/ );
diff -ruN Error-Exception-1.0/t/20-try-catch.t Error-Exception-1.1/t/20-try-catch.t
--- Error-Exception-1.0/t/20-try-catch.t 1969-12-31 16:00:00.000000000 -0800
+++ Error-Exception-1.1/t/20-try-catch.t 2008-09-03 01:23:42.000000000 -0700
@@ -0,0 +1,19 @@
+
+use strict;
+use warnings;
+
+use Test::More qw/ no_plan /;
+use Test::Exception;
+
+BEGIN { use_ok( 'Error::Exception', qw/ MyError / ); }
+
+lives_ok {
+ try { throw MyError 'An error' } catch MyError with { };
+} 'Caught simple error.';
+
+dies_ok {
+ try { throw Error 'An error' } catch MyError with { };
+} 'Simple uncaught exception.';
+
+
+