Linux and UNIX Man Pages

Linux & Unix Commands - Search Man Pages

trycatch(3pm) [debian man page]

TryCatch(3pm)						User Contributed Perl Documentation					     TryCatch(3pm)

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
o Decide on "finally" semantics w.r.t return values. o Write some more documentation o Split out the dependancy on Moose 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. Zefram for providing support and XS guidance. Xavier Bergade for the impetus to finally fix this module in 5.12. LICENSE
Licensed under the same terms as Perl itself. perl v5.14.2 2010-10-13 TryCatch(3pm)

Check Out this Related Man Page

Parse::Method::Signatures(3)				User Contributed Perl Documentation			      Parse::Method::Signatures(3)

NAME
Parse::Method::Signatures - Perl6 like method signature parser DESCRIPTION
Inspired by Perl6::Signature but streamlined to just support the subset deemed useful for TryCatch and MooseX::Method::Signatures. TODO
o Document the parameter return types. o Probably lots of other things METHODS
There are only two public methods to this module, both of which should be called as class methods. Both methods accept either a single (non-ref) scalar as the value for the "input" attribute, or normal new style arguments (hash or hash-ref). signature my $sig = Parse::Method::Signatures->signature( '(Str $foo)' ) Attempts to parse the (bracketed) method signature. Returns a value or croaks on error. param my $param = Parse::Method::Signatures->param( 'Str $foo where { length($_) < 10 }') Attempts to parse the specification for a single parameter. Returns value or croaks on error. ATTRIBUTES
All the attributes on this class are read-only. input Type: Str The string to parse. offset Type: Int Offset into "input" at which to start parsing. Useful for using with Devel::Declare linestring signature_class Default: Parse::Method::Signatures::Sig Type: Str (loaded on demand class name) param_class Default: Parse::Method::Signatures::Param Type: Str (loaded on demand class name) type_constraint_class Default: Parse::Method::Signatures::TypeConstraint Type: Str (loaded on demand class name) Class that is used to turn the parsed type constraint into an actual Moose::Meta::TypeConstraint object. from_namespace Type: ClassName Let this module know which package it is parsing signatures form. This is entirely optional, and the only effect is has is on parsing type constraints. If this attribute is set it is passed to "type_constraint_class" which can use it to introspect the package (commmonly for MooseX::Types exported types). See "find_registered_constraint" in Parse::Method::Signature::TypeConstraints for more details. type_constraint_callback Type: CodeRef Passed to the constructor of "type_constraint_class". Default implementation of this callback asks Moose for a type constrain matching the name passed in. If you have more complex requirements, such as parsing types created by MooseX::Types then you will want a callback similar to this: # my $target_package defined elsewhere. my $tc_cb = sub { my ($pms_tc, $name) = @_; my $code = $target_package->can($name); $code ? eval { $code->() } : $pms_tc->find_registered_constraint($name); } Note that the above example is better provided by providing the "from_namespace" attribute. CAVEATS
Like Perl6::Signature, the parsing of certain constructs is currently only a 'best effort' - specifically default values and where code blocks might not successfully for certain complex cases. Patches/Failing tests welcome. Additionally, default value specifications are not evaluated which means that no such lexical or similar errors will not be produced by this module. Constant folding will also not be performed. There are certain constructs that are simply too much hassle to avoid when the work around is simple. Currently the only cases that are known to parse wrong are when using anonymous variables (i.e. just sigils) in unpacked arrays. Take the following example: method foo (ArrayRef [$, $], $some_value_we_care_about) { In this case the $] is treated as one of perl's magic variables (specifically, the patch level of the Perl interpreter) rather than a "$" followed by a "]" as was almost certainly intended. The work around for this is simple: introduce a space between the charcters: method foo (ArrayRef [ $, $ ], $some_value_we_care_about) { The same applies AUTHOR
Ash Berlin <ash@cpan.org>. Thanks to Florian Ragwitz <rafl@debian.org>. Many thanks to Piers Cawley to showing me the way to refactor my spaghetti code into something more manageable. SEE ALSO
Devel::Declare which is used by most modules that use this (currently by all modules known to the author.) <http://github.com/ashb/trycatch/tree>. LICENSE
Licensed under the same terms as Perl itself. This distribution copyright 2008-2009, Ash Berlin <ash@cpan.org> perl v5.16.2 2011-09-09 Parse::Method::Signatures(3)
Man Page