Try-Tiny-SmartCatch-0.5/000755 000765 000024 00000000000 12071344527 015256 5ustar00myszstaff000000 000000 Try-Tiny-SmartCatch-0.5/Changes000644 000765 000024 00000001330 12071344240 016536 0ustar00myszstaff000000 000000 Revision history for Try-Tiny-SmartCatch 0.5 2013-01-03 - missing Changes description 0.4 - added throw function - rewritten source 0.3 - do not use features from perl 5.10, maybe it repair working module on 5.8.x - added 'then' block handling - removed calling try blocks in same context as try subroutine 0.2 2012-05-24 Few changes suggested by: mst - Matt S Trout (cpan:MSTROUT) (thanks!): - rename from Try::Tiny::Extended to Try::Tiny::SmartCatch - rename function Try::Tiny::SmartCatch::catch -> Try::Tiny::SmartCatch::catch_when - rename function Try::Tiny::SmartCatch::catch_all -> Try::Tiny::SmartCatch::catch_default 0.1 2012-04-30 Initial version. Try-Tiny-SmartCatch-0.5/lib/000755 000765 000024 00000000000 12071344526 016023 5ustar00myszstaff000000 000000 Try-Tiny-SmartCatch-0.5/Makefile.PL000644 000765 000024 00000001376 12071340377 017236 0ustar00myszstaff000000 000000 use 5.006; use strict; use warnings; use ExtUtils::MakeMaker; WriteMakefile( NAME => 'Try::Tiny::SmartCatch', AUTHOR => q{Marcin Sztolcman }, VERSION_FROM => 'lib/Try/Tiny/SmartCatch.pm', ABSTRACT_FROM => 'lib/Try/Tiny/SmartCatch.pm', ($ExtUtils::MakeMaker::VERSION >= 6.3002 ? ('LICENSE'=> 'MIT') : ()), PL_FILES => {}, PREREQ_PM => { 'Test::More' => 0, }, dist => { COMPRESS => 'gzip -9f', SUFFIX => 'gz', }, clean => { FILES => 'Try-Tiny-SmartCatch-*' }, META_MERGE => { resources => { repository => 'https://github.com/mysz/try-tiny-smartcatch', }, }, ); Try-Tiny-SmartCatch-0.5/MANIFEST000644 000765 000024 00000000456 12071344527 016414 0ustar00myszstaff000000 000000 Changes lib/Try/Tiny/SmartCatch.pm Makefile.PL MANIFEST This list of files README t/00-load.t t/manifest.t t/pod-coverage.t t/pod.t META.yml Module YAML meta-data (added by MakeMaker) META.json Module JSON meta-data (added by MakeMaker) Try-Tiny-SmartCatch-0.5/META.json000644 000765 000024 00000002007 12071344527 016676 0ustar00myszstaff000000 000000 { "abstract" : "lightweight Perl module for powerful exceptions handling", "author" : [ "Marcin Sztolcman " ], "dynamic_config" : 1, "generated_by" : "ExtUtils::MakeMaker version 6.62, CPAN::Meta::Converter version 2.120921", "license" : [ "mit" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : "2" }, "name" : "Try-Tiny-SmartCatch", "no_index" : { "directory" : [ "t", "inc" ] }, "prereqs" : { "build" : { "requires" : { "ExtUtils::MakeMaker" : "0" } }, "configure" : { "requires" : { "ExtUtils::MakeMaker" : "0" } }, "runtime" : { "requires" : { "Test::More" : "0" } } }, "release_status" : "stable", "resources" : { "repository" : { "url" : "https://github.com/mysz/try-tiny-smartcatch" } }, "version" : "0.5" } Try-Tiny-SmartCatch-0.5/META.yml000644 000765 000024 00000001123 12071344526 016523 0ustar00myszstaff000000 000000 --- abstract: 'lightweight Perl module for powerful exceptions handling' author: - 'Marcin Sztolcman ' build_requires: ExtUtils::MakeMaker: 0 configure_requires: ExtUtils::MakeMaker: 0 dynamic_config: 1 generated_by: 'ExtUtils::MakeMaker version 6.62, CPAN::Meta::Converter version 2.120921' license: mit meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: 1.4 name: Try-Tiny-SmartCatch no_index: directory: - t - inc requires: Test::More: 0 resources: repository: https://github.com/mysz/try-tiny-smartcatch version: 0.5 Try-Tiny-SmartCatch-0.5/README000644 000765 000024 00000002515 12071340377 016140 0ustar00myszstaff000000 000000 Try-Tiny-SmartCatch Lightweight Perl module for powerful exceptions handling. Provides set of functions (by default): * try * catch_when * catch_default * then * finally Also you can request to import "throw" function (currently it is an alias for "die", but more explicit): use Try::Tiny::Extended qw/:all/; Try::Tiny::SmartCatch give you the same posibilities as Try::Tiny, but also allow you to catch only certain types of exceptions. Additionally change a little it's syntax to use explicit sub blocks. INSTALLATION To install this module, run the following commands: perl Makefile.PL make make test make install Or just: cpan Try::Catch::SmartCatch :) SUPPORT AND DOCUMENTATION After installing, you can find documentation for this module with the perldoc command. perldoc Try::Tiny::SmartCatch You can also look for information at: Try::Tiny::SmartCatch home & source code https://github.com/mysz/try-tiny-smartcatch Issue tracker (report bugs here) https://github.com/mysz/try-tiny-smartcatch/issues LICENSE AND COPYRIGHT Copyright (c) 2012 Marcin Sztolcman. All rights reserved. Base code is borrowed from Yuval Kogman L module, released under MIT License. This program is free software; you can redistribute it and/or modify it under the terms of the MIT license. Try-Tiny-SmartCatch-0.5/t/000755 000765 000024 00000000000 12071344526 015520 5ustar00myszstaff000000 000000 Try-Tiny-SmartCatch-0.5/t/00-load.t000644 000765 000024 00000000307 11757520166 017047 0ustar00myszstaff000000 000000 #!perl -T use Test::More tests => 1; BEGIN { use_ok( 'Try::Tiny::SmartCatch' ) || print "Bail out!\n"; } diag( "Testing Try::Tiny::SmartCatch $Try::Tiny::SmartCatch::VERSION, Perl $], $^X" ); Try-Tiny-SmartCatch-0.5/t/manifest.t000644 000765 000024 00000000420 11757524511 017513 0ustar00myszstaff000000 000000 #!perl -T use strict; use warnings; use Test::More; unless ( $ENV{RELEASE_TESTING} ) { plan( skip_all => "Author tests not required for installation" ); } eval "use Test::CheckManifest 0.9"; plan skip_all => "Test::CheckManifest 0.9 required" if $@; ok_manifest(); Try-Tiny-SmartCatch-0.5/t/pod-coverage.t000644 000765 000024 00000001047 11757524513 020270 0ustar00myszstaff000000 000000 use strict; use warnings; use Test::More; # Ensure a recent version of Test::Pod::Coverage my $min_tpc = 1.08; eval "use Test::Pod::Coverage $min_tpc"; plan skip_all => "Test::Pod::Coverage $min_tpc required for testing POD coverage" if $@; # Test::Pod::Coverage doesn't require a minimum Pod::Coverage version, # but older versions don't recognize some common documentation styles my $min_pc = 0.18; eval "use Pod::Coverage $min_pc"; plan skip_all => "Pod::Coverage $min_pc required for testing POD coverage" if $@; all_pod_coverage_ok(); Try-Tiny-SmartCatch-0.5/t/pod.t000644 000765 000024 00000000350 11757524515 016475 0ustar00myszstaff000000 000000 #!perl -T use strict; use warnings; use Test::More; # Ensure a recent version of Test::Pod my $min_tp = 1.22; eval "use Test::Pod $min_tp"; plan skip_all => "Test::Pod $min_tp required for testing POD" if $@; all_pod_files_ok(); Try-Tiny-SmartCatch-0.5/lib/Try/000755 000765 000024 00000000000 12071344526 016601 5ustar00myszstaff000000 000000 Try-Tiny-SmartCatch-0.5/lib/Try/Tiny/000755 000765 000024 00000000000 12071344526 017524 5ustar00myszstaff000000 000000 Try-Tiny-SmartCatch-0.5/lib/Try/Tiny/SmartCatch.pm000644 000765 000024 00000032526 12071344336 022122 0ustar00myszstaff000000 000000 package Try::Tiny::SmartCatch; use 5.006; use strict; use warnings; use vars qw/@EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION @ISA/; BEGIN { require Exporter; @ISA = qw/Exporter/; } @EXPORT = qw/try catch_when catch_default then finally/; @EXPORT_OK = (@EXPORT, qw/throw/); %EXPORT_TAGS = ( all => [@EXPORT_OK], ); ++$Carp::Internal{+__PACKAGE__}; $VERSION = '0.5'; sub try($;@) { my ($try, @code_refs) = @_; my ($catch_default, @catch_when, $code_ref, @finally, $ref_type, $then, $wantarray); $wantarray = wantarray (); foreach $code_ref (@code_refs) { next if (!$code_ref); $ref_type = ref($code_ref); ## zero or more 'catch_when' blocks if ($ref_type eq 'Try::Tiny::SmartCatch::Catch::When') { ## we need to save same handler for many different exception types push(@catch_when, $code_ref); } ## zero or one 'catch_default' blocks elsif ($ref_type eq 'Try::Tiny::SmartCatch::Catch::Default') { $catch_default = $$code_ref{code} if (!defined($catch_default)); } ## zero or more 'finally' blocks elsif ($ref_type eq 'Try::Tiny::SmartCatch::Finally') { push(@finally, $$code_ref); } ## zero or one 'then' blocks elsif ($ref_type eq 'Try::Tiny::SmartCatch::Then') { $then = $$code_ref if (!defined($then)); } ## unknown block type else { require Carp; Carp::confess("Unknown code ref type given '$ref_type'. Check your usage & try again"); } } my ($error, $failed, $prev_error, @ret); ## save the value of $@ so we can set $@ back to it in the beginning of the eval $prev_error = $@; { ## localize $@ to prevent clobbering of previous value by a successful eval. local $@; ## failed will be true if the eval dies, because 1 will not be returned from the eval body $failed = not eval { $@ = $prev_error; ## call try block in list context if try subroutine is called in list context, or we have 'then' block ## result of 'try' block is passed as arguments to then block, so we need do that in that way if ($wantarray || $then) { @ret = &$try(); } elsif (defined($wantarray)) { $ret[0] = &$try(); } else { &$try(); } ## properly set $fail to false return 1; }; ## copy $@ to $error; when we leave this scope, local $@ will revert $@ ## back to its previous value $error = $@; } ## set up a scope guard to invoke the finally block at the end my @guards = ( map { Try::Tiny::SmartCatch::ScopeGuard->_new($_, $failed ? $error : ()) } @finally ); ## at this point $failed contains a true value if the eval died, even if some ## destructor overwrote $@ as the eval was unwinding. if ($failed) { ## if we got an error, invoke the catch block. if (scalar(@catch_when) || $catch_default) { ## This works like given($error), but is backwards compatible and ## sets $_ in the dynamic scope for the body of $catch for ($error) { my ($catch_data); foreach $catch_data (@catch_when) { return &{$$catch_data{code}}($error) if ($catch_data->for_error($error)); } return &$catch_default($error) if ($catch_default); die($error); } } return; } ## no failure, $@ is back to what it was, everything is fine else { ## do we have then block? if we does, execute it in correct context if ($then) { if ($wantarray) { @ret = &$then(@ret); } elsif (defined($wantarray)) { $ret[0] = &$then(@ret); } else { &$then(@ret); } } return if (!defined($wantarray)); return $wantarray ? @ret : $ret[0]; } } sub catch_when ($$;@) { my ($types, $block) = (shift(@_), shift(@_)); my $catch = Try::Tiny::SmartCatch::Catch::When->new($block, $types); return ($catch, @_); } sub catch_default ($;@) { my $block = shift(@_); my $catch = Try::Tiny::SmartCatch::Catch::Default->new($block); return ($catch, @_); } sub then ($;@) { my $block = shift(@_); my $then = bless(\$block, 'Try::Tiny::SmartCatch::Then'); return ($then, @_); } sub finally ($;@) { my $block = shift(@_); my $finally = bless(\$block, 'Try::Tiny::SmartCatch::Finally'); return ($finally, @_); } sub throw { return die (@_); } package # hide from PAUSE Try::Tiny::SmartCatch::ScopeGuard; { sub _new { shift(@_); return bless([ @_ ]); } sub DESTROY { my ($guts) = @_; my $code = shift(@$guts); return &$code(@$guts); } } package Try::Tiny::SmartCatch::Catch::Default; { sub new { my ($class, $code) = @_; my $self = { code => $code }; $self = bless($self, $class); return $self; } } package Try::Tiny::SmartCatch::Catch::When; { use Scalar::Util qw/blessed/; sub new { my ($class, $code, $types) = @_; my $self = { code => $code, types => ( ref($types) eq 'ARRAY' ? $types : defined($types) ? [$types] : [] ), }; return bless($self, $class); } sub for_error { my ($self, $error, $types) = @_; $types = $$self{types} if (!defined($types)); $types = [$types] if (ref($types) ne 'ARRAY'); if (blessed($error)) { foreach (@$types) { return 1 if ($error->isa($_)); } } else { my $type; foreach $type (@$types) { return 1 if ( (ref($type) eq 'Regexp' && $error =~ /$type/) || (!ref($type) && index($error, $type) > -1) ); } } return; } } 1; __END__ =head1 NAME Try::Tiny::SmartCatch - lightweight Perl module for powerful exceptions handling =head1 VERSION Version 0.5 =head1 SYNOPSIS use Try::Tiny::SmartCatch; try sub {}, # at least one try block catch_when 'ExceptionName' => sub {}, # zero or more catch_when blocks catch_when 'exception message' => sub {}, catch_when qr/exception message regexp/ => sub {}, catch_default sub {}, # zero or one catch_default block then sub {}, # if no exception is raised, execute then block finally sub {}; #zero or more finally blocks use Try::Tiny::SmartCatch qw/throw/; # import only throw # You can import also all function at once: # use Try::Tiny::SmartCatch qw/:all/; throw('some exception'); throw(SomeException->new ('message')); =head1 DESCRIPTION Goals are mostly the same as L module, but there are few changes to it's specification. Main difference is possibility to catch just some kinds of exceptions in place of catching everything. Another one is slightly changed syntax. When raised exception is an object, L will test for exception type (using C). When raised exception is just a text message (like: C), there can be specified part of message to test for. There are also explicit C blocks. In opposite to C, every block in C: C, C, C, C and C must have explicit subroutines specified. Thanks to trick with function prototype, calling C or C creates implicit subroutines: sub test_function { try { # yes, here is implicit subroutine! # return statement here exits just from try block, # not from test_function! return 1; }; say 'Hello!'; } test_function(); Above snippet produces us text on STDOUT: C But more obvious would be no output... (by C statement). This is because of implicit subroutine created with braces: C<{}> after C, C or C from C. C is more explicit - you must always use C when defining blocks (look at [Syntax](#Syntax) above). An exception object or message is passed to defined blocks in two ways: * in C<$_> variable * as function arguments, so through C<@_> array. L defines also C function (not imported by default). Currently it is an alias for C, but is more explicit then C :) It can be imported separately: use Try::Tiny::SmartCatch qw/throw/; Or with rest of functions: use Try::Tiny::SmartCatch qw/:all/; =head1 EXPORT By default exported are functions: =over =item try =item catch_when =item catch_default =item then =item finally =back You can also explicit import C function: use Try::Tiny::SmartCatch qw/throw/; Or all functions at all: use Try::Tiny::SmartCatch qw/:all/; =head1 SUBROUTINES/METHODS =head2 try($;@) Works like L C subroutine, here is nothing to add :) The only difference is that here must be given evident sub reference, not anonymous block: try sub { # some code }; =head2 catch_when($$;@) Intended to be used in the second argument position of C. Works similarly to L C subroutine, but have a little different syntax: try sub { # some code }, catch_when 'Exception1' => sub { # catch only Exception1 exception }, catch_when ['Exception1', 'Exception2'] => sub { # catch Exception2 or Exception3 exceptions }; If raised exception is a blessed reference (or object), C means that exception class has to be or inherits from C class. In other case, it search for given string in exception message (using C function or regular expressions - depending on type of given operator). For example: try sub { throw('some exception message'); }, catch_when 'exception' => sub { say 'exception caught!'; }; Other case: try sub { throw('some exception3 message'); }, catch_when qr/exception\d/ => sub { say 'exception caught!'; }; Or: try sub { # ValueError extends RuntimeError throw(ValueError->new ('Some error message')); }, catch_when 'RuntimeError' => sub { say 'RuntimeError exception caught!'; }; =head2 catch_default($;@) Works exactly like L C function (OK, there is difference: need to specify evident sub block instead of anonymous block): try sub { # some code }, catch_default sub { say 'caught every exception'; }; =head2 then($;@) C block is executed after C clause, if none of C or C blocks was executed (it means, if no exception occured). It's executed before C blocks. try sub { # some code }, catch_when 'MyException' => sub { say 'caught MyException exception'; }, then sub { say 'No exception was raised'; }, finally sub { say 'executed always'; }; =head2 finally($;@) Works exactly like L C function (OK, again, explicit sub instead of implicit): try sub { # some code }, finally sub { say 'executed always'; }; =head2 throw Currently it's an alias to C function, but C is more obvious then C when working with exceptions :) In future it also can do more then just call C. It's not exported by default (see: L) =head1 SEE ALSO =over 4 =item L Try::Tiny::SmartCatch home. =item L Minimal try/catch with proper localization of $@, base of L =item L First class try catch semantics for Perl, without source filters. =back =head1 AUTHOR Marcin Sztolcman, C<< >> =head1 BUGS Please report any bugs or feature requests through the web interface at L. =head1 SUPPORT You can find documentation for this module with the perldoc command. perldoc Try::Tiny::SmartCatch You can also look for information at: =over 4 =item * Try::Tiny::SmartCatch home & source code L =item * Issue tracker (report bugs here) L =item * Search CPAN L =back =head1 ACKNOWLEDGEMENTS =over =item Yuval Kogman for his L module =item mst - Matt S Trout (cpan:MSTROUT) for good package name and few great features =back =head1 LICENSE AND COPYRIGHT Copyright (c) 2012-2013 Marcin Sztolcman. All rights reserved. Base code is borrowed from Yuval Kogman L module, released under MIT License. This program is free software; you can redistribute it and/or modify it under the terms of the MIT license. =cut