Data::Object::Try
Data-Object Try/Catch Class
use Data::Object::Try; my $try = Data::Object::Try->new; $try->call(fun (@args) { # try something return something }); $try->catch($type, fun ($caught) { # caught an exception return $something; }); $try->default(fun ($caught) { # catch the uncaught return $something; }); $try->finally(fun (@args) { # always run after try/catch }); my $result = $try->result(@args);
This package provides an object-oriented interface for performing complex try/catch operations.
This package has the following attributes.
invocant(Object)
The attribute is read-only, accepts (Object) values, and is optional.
(Object)
arguments(ArrayRef)
The attribute is read-only, accepts (ArrayRef) values, and is optional.
(ArrayRef)
on_try(CodeRef)
The attribute is read-only, accepts (CodeRef) values, and is optional.
(CodeRef)
on_catch(ArrayRef[CodeRef])
The attribute is read-only, accepts (ArrayRef[CodeRef]) values, and is optional.
(ArrayRef[CodeRef])
on_default(CodeRef)
on_finally(CodeRef)
This package implements the following methods.
call(Str | CodeRef $method) : Object
The call method takes a method name or coderef, registers it as the tryable routine, and returns the object. When invoked, the callback will received an invocant if one was provided to the constructor, the default arguments if any were provided to the constructor, and whatever arguments were provided by the invocant.
invocant
arguments
$try = $try->call($method); $try = $try->call(fun (@args) { # do something });
callback(Str | CodeRef) : CodeRef
The callback method takes a method name or coderef, and returns a coderef for registration. If a coderef is provided this method is mostly a passthrough.
my $callback; $callback = $try->callback($method); $callback = $try->callback(fun (@args) { # do something });
catch(Str $isa, Str | CodeRef $callback) : Any
The catch method takes a package or ref name, and when triggered checks whether the captured exception is of the type specified and if so executes the given callback.
$try = $try->catch('Error::HTTP400', fun ($caught) { # do something }); $try = $try->catch('Error::HTTP401', fun ($caught) { # do something });
default(Str | CodeRef $callback) : Object
The default method takes a method name or coderef and is triggered if no catch conditions match the exception thrown.
catch
$try = $try->default(fun ($caught) { # do something });
execute(CodeRef $callback, Any @args) : Any
The execute method takes a coderef and executes it with any given arguments. When invoked, the callback will received an invocant if one was provided to the constructor, the default arguments if any were provided to the constructor, and whatever arguments were passed directly to this method.
my $result = $try->execute($callback, @args);
finally(Str | CodeRef $callback) : Object
The finally method takes a package or ref name and always executes the callback after a try/catch operation. The return value is ignored. When invoked, the callback will received an invocant if one was provided to the constructor, the default arguments if any were provided to the constructor, and whatever arguments were provided by the invocant.
$try = $try->finally(fun (@args) { # always do something });
no_catch() : Object
The no_catch method removes any configured catch conditions and returns the object.
$try = $try->no_catch;
no_default() : Object
The no_default method removes any configured default condition and returns the object.
$try = $try->no_default;
no_finally() : Object
The no_finally method removes any configured finally condition and returns the object.
$try = $try->no_finally;
no_try() : Object
The no_try method removes any configured "try" operation and returns the object.
$try = $try->no_try;
result(Any @args) : Any
The result method executes the try/catch/default/finally logic and returns either 1) the return value from the successfully tried operation 2) the return value from the successfully matched catch condition if an exception was thrown 3) the return value from the default catch condition if an exception was thrown and no catch condition matched. When invoked, the try and finally callbacks will received an invocant if one was provided to the constructor, the default arguments if any were provided to the constructor, and whatever arguments were passed directly to this method.
try
finally
my $result = $try->result(@args);
Al Newkirk, +296
+296
Anthony Brummett, +10
+10
José Joaquín Atria, +1
+1
Al Newkirk, awncorp@cpan.org
awncorp@cpan.org
Copyright (C) 2011-2019, Al Newkirk, et al.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
GitHub
Projects
Milestones
Contributing
Issues
To get the most out of this distribution, consider reading the following:
Do
Data::Object
Data::Object::Class
Data::Object::ClassHas
Data::Object::Role
Data::Object::RoleHas
Data::Object::Library
To install Do, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Do
CPAN shell
perl -MCPAN -e shell install Do
For more information on module installation, please visit the detailed CPAN module installation guide.