Data::Object::Try
Try Class for Perl 5
method: call method: callback method: catch method: default method: execute method: finally method: maybe method: no_catch method: no_default method: no_finally method: no_try method: result
use strict;
use warnings;
use routines;
use Data::Object::Try;
my $try = Data::Object::Try->new;
$try->call(fun (@args) {
# try something
return time;
});
$try->catch('Example::Exception', fun ($caught) {
# caught an exception
return;
});
$try->default(fun ($caught) {
# catch the uncaught
return;
});
$try->finally(fun (@args) {
# always run after try/catch
return;
});
my @args;
my $result = $try->result(@args);
invocant: ro, opt, Object arguments: ro, opt, ArrayRef on_try: rw, opt, CodeRef on_catch: rw, opt, ArrayRef[CodeRef] on_default: rw, opt, CodeRef on_finally: rw, opt, CodeRef
This package provides an object-oriented interface for performing complex try/catch operations.
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.
call(Str | CodeRef $arg) : Object
=example-1 call
use routines;
my $try = Data::Object::Try->new;
$try->call(fun (@args) {
return [@args];
});
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.
callback(Str | CodeRef $arg) : CodeRef
=example-1 callback
use routines;
my $try = Data::Object::Try->new;
$try->callback(fun (@args) {
return [@args];
});
=example-2 callback
package Example;
use Moo;
use routines;
fun test(@args) {
return [@args];
}
package main;
my $try = Data::Object::Try->new(
invocant => Example->new
);
$try->callback('test');
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.
catch(Str $isa, Str | CodeRef $arg) : Any
=example-1 catch
use routines;
my $try = Data::Object::Try->new;
$try->call(fun (@args) {
die $try;
});
$try->catch('Data::Object::Try', fun (@args) {
return [@args];
});
The default method takes a method name or coderef and is triggered if no catch conditions match the exception thrown.
default(Str | CodeRef $arg) : Object
=example-1 default
use routines;
my $try = Data::Object::Try->new;
$try->call(fun (@args) {
die $try;
});
$try->default(fun (@args) {
return [@args];
});
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.
execute(CodeRef $arg, Any @args) : Any
=example-1 execute
use routines;
my $try = Data::Object::Try->new(
invocant => Example->new,
arguments => [1,2,3]
);
$try->execute(fun (@args) {
return [@args];
});
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.
finally(Str | CodeRef $arg) : Object
=example-1 finally
use routines;
my $try = Data::Object::Try->new(
invocant => Example->new,
arguments => [1,2,3]
);
$try->call(fun (@args) {
return $try;
});
$try->finally(fun (@args) {
$try->{'$finally'} = [@args];
});
The maybe method registers a default catch condition that returns falsy, i.e. an empty string, if an exception is encountered.
maybe() : Object
=example-1 maybe
use routines;
my $try = Data::Object::Try->new;
$try->call(fun (@args) {
die $try;
});
$try->maybe;
The no_catch method removes any configured catch conditions and returns the object.
no_catch() : Object
=example-1 no_catch
use routines;
my $try = Data::Object::Try->new;
$try->call(fun (@args) {
die $try;
});
$try->catch('Data::Object::Try', fun (@args) {
return [@args];
});
$try->no_catch;
The no_default method removes any configured default condition and returns the object.
no_default() : Object
=example-1 no_default
use routines;
my $try = Data::Object::Try->new;
$try->call(fun (@args) {
die $try;
});
$try->default(fun (@args) {
return [@args];
});
$try->no_default;
The no_finally method removes any configured finally condition and returns the object.
no_finally() : Object
=example-1 no_finally
use routines;
my $try = Data::Object::Try->new(
invocant => Example->new,
arguments => [1,2,3]
);
$try->call(fun (@args) {
return $try;
});
$try->finally(fun (@args) {
$try->{'$finally'} = [@args];
});
$try->no_finally;
The no_try method removes any configured try operation and returns the object.
no_try() : Object
=example-1 no_try
use routines;
my $try = Data::Object::Try->new;
$try->call(fun (@args) {
return [@args];
});
$try->no_try;
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.
result(Any @args) : Any
=example-1 result
use routines;
my $try = Data::Object::Try->new;
$try->call(fun (@args) {
return [@args];
});
$try->result;
=example-2 result
use routines;
my $try = Data::Object::Try->new;
$try->call(fun (@args) {
return [@args];
});
$try->result(1..5);
30 POD Errors
The following errors were encountered while parsing the POD:
- Around line 10:
Unknown directive: =name
- Around line 16:
Unknown directive: =abstract
- Around line 22:
Unknown directive: =includes
- Around line 39:
Unknown directive: =synopsis
- Around line 79:
Unknown directive: =attributes
- Around line 90:
Unknown directive: =description
- Around line 96:
Unknown directive: =method
- Around line 104:
Unknown directive: =signature
- Around line 121:
Unknown directive: =method
- Around line 126:
Unknown directive: =signature
- Around line 163:
Unknown directive: =method
- Around line 169:
Unknown directive: =signature
- Around line 191:
Unknown directive: =method
- Around line 196:
Unknown directive: =signature
- Around line 218:
Unknown directive: =method
- Around line 225:
Unknown directive: =signature
- Around line 245:
Unknown directive: =method
- Around line 253:
Unknown directive: =signature
- Around line 278:
Unknown directive: =method
- Around line 283:
Unknown directive: =signature
- Around line 302:
Unknown directive: =method
- Around line 307:
Unknown directive: =signature
- Around line 331:
Unknown directive: =method
- Around line 336:
Unknown directive: =signature
- Around line 360:
Unknown directive: =method
- Around line 365:
Unknown directive: =signature
- Around line 392:
Unknown directive: =method
- Around line 397:
Unknown directive: =signature
- Around line 416:
Unknown directive: =method
- Around line 427:
Unknown directive: =signature