public
Description: 'Native' try {} catch {} semantics for perl using Devel::Declare
Homepage:
Clone URL: git://github.com/ashb/trycatch.git
name age message
file .gitignore Thu Mar 26 05:04:22 -0700 2009 Update ignores [ashb]
file Changes Wed Sep 09 14:15:28 -0700 2009 Version 1.001001_999 [ashb]
file MANIFEST.SKIP Mon Mar 16 15:09:25 -0700 2009 Release a version that works with more recent P... [ashb]
file Makefile.PL Wed Sep 09 14:15:28 -0700 2009 Version 1.001001_999 [ashb]
file README Sat Aug 08 14:46:20 -0700 2009 Update pod a bit [ashb]
file TryCatch.xs Wed Sep 09 13:36:42 -0700 2009 When CvISXSUB() is not present, one should chec... [Vincent Pit]
file bench.pl Sat Apr 11 21:32:53 -0700 2009 Run all the benchmarks we can [ashb]
file bench_ok.pl Sat Apr 11 20:48:50 -0700 2009 More speed up tweaking. things dont work right yet [ashb]
directory lib/ Wed Sep 09 15:08:06 -0700 2009 Fallback to treating str TCs as class names [ashb]
file ppport.h Tue May 12 02:10:42 -0700 2009 Use pppport.h and its suggested changes. [ashb]
directory t/ Wed Sep 09 13:42:58 -0700 2009 Add test for problem reported by Mark Fowler, a... [ashb]
README
NAME
    TryCatch - first class try catch semantics for Perl, without source
    filters.

DESCRIPTION
    This module aims to provide a nicer syntax and method to catch errors in
    Perl, similar to what is found in other languages (such as Java, Python
    or C++). The standard method of using "eval {}; if ($@) {}" is often
    prone to subtle bugs, primarily that its far too easy to stomp on the
    error in error handlers. And also eval/if isn't the nicest idiom.

SYNOPSIS
     use TryCatch;

     sub foo {
       my ($self) = @_;

       try {
         die Some::Class->new(code => 404 ) if $self->not_found;
         return "return value from foo";
       }
       catch (Some::Class $e where { $_->code > 100 } ) {
       }
     }

SYNTAX
    This module aims to give first class exception handling to perl via
    'try' and 'catch' keywords. The basic syntax this module provides is
    "try { # block }" followed by zero or more catch blocks. Each catch
    block has an optional type constraint on it the resembles Perl6's method
    signatures.

    Also worth noting is that the error variable ($@) is localised to the
    try/catch blocks and will not leak outside the scope, or stomp on a
    previous value of $@.

    The simplest case of a catch block is just

     catch { ... }

    where upon the error is available in the standard $@ variable and no
    type checking is performed. The exception can instead be accessed via a
    named lexical variable by providing a simple signature to the catch
    block as follows:

     catch ($err) { ... }

    Type checking of the exception can be performed by specifing a type
    constraint or where clauses in the signature as follows:

     catch (TypeFoo $e) { ... }
     catch (Dict[code => Int, message => Str] $err) { ... }

    As shown in the above example, complex Moose types can be used,
    including MooseX::Types style of type constraints

    In addition to type checking via Moose type constraints, you can also
    use where clauses to only match a certain sub-condition on an error. For
    example, assuming that "HTTPError" is a suitably defined TC:

     catch (HTTPError $e where { $_->code >= 400 && $_->code <= 499 } ) { 
       return "4XX error";
     }
     catch (HTTPError $e) {
       return "other http code";
     }

    would return "4XX error" in the case of a 404 error, and "other http
    code" in the case of a 302.

    In the case where multiple catch blocks are present, the first one that
    matches the type constraints (if any) will executed.

BENEFITS
    return. You can put a return in a try block, and it would do the right
    thing - namely return a value from the subroutine you are in, instead of
    just from the eval block.

    Type Checking. This is nothing you couldn't do manually yourself, it
    does it for you using Moose type constraints.

TODO
    *   Decide on "finally" semantics w.r.t return values.

    *   Write some more documentation

SEE ALSO
    MooseX::Types, Moose::Util::TypeConstraints, Parse::Method::Signatures.

AUTHOR
    Ash Berlin <ash@cpan.org>

THANKS
    Thanks to Matt S Trout and Florian Ragwitz for work on Devel::Declare
    and various B::Hooks modules

    Vincent Pit for Scope::Upper that makes the return from block possible.

LICENSE
    Licensed under the same terms as Perl itself.