Venus::Try - Try Class
Try Class for Perl 5
package main; use Venus::Try; my $try = Venus::Try->new; $try->call(sub { my (@args) = @_; # try something return time; }); $try->catch('Example::Error', sub { my ($caught) = @_; # caught an error (exception) return; }); $try->default(sub { my ($caught) = @_; # catch the uncaught return; }); $try->finally(sub { my (@args) = @_; # always run after try/catch return; }); my @args; 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)
This attribute is read-only, accepts (Object) values, and is optional.
(Object)
arguments(ArrayRef)
This attribute is read-only, accepts (ArrayRef) values, and is optional.
(ArrayRef)
on_try(CodeRef)
This attribute is read-write, accepts (CodeRef) values, and is optional.
(CodeRef)
on_catch(ArrayRef[CodeRef])
This attribute is read-write, accepts (ArrayRef[CodeRef]) values, is optional, and defaults to [].
(ArrayRef[CodeRef])
[]
on_default(CodeRef)
on_finally(CodeRef)
This package inherits behaviors from:
Venus::Kind::Utility
This package provides the following methods:
call(Str | CodeRef $method) (Try)
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
Since 0.01
0.01
package main; use Venus::Try; my $try = Venus::Try->new; my $call = $try->call(sub { my (@args) = @_; return [@args]; }); # bless({ on_catch => ... }, "Venus::Try")
callback(Str | CodeRef $method) (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.
package main; use Venus::Try; my $try = Venus::Try->new; my $callback = $try->callback(sub { my (@args) = @_; return [@args]; }); # sub { ... }
package Example1; sub new { bless {}; } sub test { my (@args) = @_; return [@args]; } package main; use Venus::Try; my $try = Venus::Try->new( invocant => Example1->new, ); my $callback = $try->callback('test'); # sub { ... }
package main; use Venus::Try; my $try = Venus::Try->new; my $callback = $try->callback('missing_method'); # Exception! Venus::Try::Error (isa Venus::Error)
catch(Str $isa, Str | CodeRef $method) (Try)
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.
package main; use Venus::Try; my $try = Venus::Try->new; $try->call(sub { my (@args) = @_; die $try; }); my $catch = $try->catch('Venus::Try', sub { my (@args) = @_; return [@args]; }); # bless({ on_catch => ... }, "Venus::Try")
default(Str | CodeRef $method) (Try)
The default method takes a method name or coderef and is triggered if no catch conditions match the exception thrown.
catch
package main; use Venus::Try; my $try = Venus::Try->new; $try->call(sub { my (@args) = @_; die $try; }); my $default = $try->default(sub { my (@args) = @_; return [@args]; }); # bless({ on_catch => ... }, "Venus::Try")
error(Ref $variable) (Try)
The error method takes a scalar reference and assigns any uncaught exceptions to it during execution.
package main; use Venus::Try; my $try = Venus::Try->new; $try->call(sub { my (@args) = @_; die $try; }); my $error = $try->error(\my $object); # bless({ on_catch => ... }, "Venus::Try")
execute(CodeRef $code, 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. This method can return a list of values in list-context.
package Example2; sub new { bless {}; } package main; use Venus::Try; my $try = Venus::Try->new( invocant => Example2->new, arguments => [1,2,3], ); my $execute = $try->execute(sub { my (@args) = @_; return [@args]; }); # [bless({}, "Example2"), 1, 2, 3]
finally(Str | CodeRef $method) (Try)
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.
package Example3; sub new { bless {}; } package main; use Venus::Try; my $try = Venus::Try->new( invocant => Example3->new, arguments => [1,2,3], ); $try->call(sub { my (@args) = @_; return $try; }); my $finally = $try->finally(sub { my (@args) = @_; $try->{args} = [@args]; }); # bless({ on_catch => ... }, "Venus::Try")
maybe() (Try)
The maybe method registers a default catch condition that returns falsy, i.e. an empty string, if an exception is encountered.
package main; use Venus::Try; my $try = Venus::Try->new; $try->call(sub { my (@args) = @_; die $try; }); my $maybe = $try->maybe; # bless({ on_catch => ... }, "Venus::Try")
no_catch() (Try)
The no_catch method removes any configured catch conditions and returns the object.
package main; use Venus::Try; my $try = Venus::Try->new; $try->call(sub { my (@args) = @_; die $try; }); $try->catch('Venus::Try', sub { my (@args) = @_; return [@args]; }); my $no_catch = $try->no_catch; # bless({ on_catch => ... }, "Venus::Try")
no_default() (Try)
The no_default method removes any configured default condition and returns the object.
package main; use Venus::Try; my $try = Venus::Try->new; $try->call(sub { my (@args) = @_; die $try; }); my $default = $try->default(sub { my (@args) = @_; return [@args]; }); my $no_default = $try->no_default; # bless({ on_catch => ... }, "Venus::Try")
no_finally() (Try)
The no_finally method removes any configured finally condition and returns the object.
package Example4; sub new { bless {}; } package main; use Venus::Try; my $try = Venus::Try->new( invocant => Example4->new, arguments => [1,2,3], ); $try->call(sub { my (@args) = @_; return $try; }); $try->finally(sub { my (@args) = @_; $try->{args} = [@args]; }); my $no_finally = $try->no_finally; # bless({ on_catch => ... }, "Venus::Try")
no_try() (Try)
The no_try method removes any configured try operation and returns the object.
try
package main; use Venus::Try; my $try = Venus::Try->new; $try->call(sub { my (@args) = @_; return [@args]; }); my $no_try = $try->no_try; # bless({ on_catch => ... }, "Venus::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. This method can return a list of values in list-context.
finally
package main; use Venus::Try; my $try = Venus::Try->new; $try->call(sub { my (@args) = @_; return [@args]; }); my $result = $try->result; # []
package main; use Venus::Try; my $try = Venus::Try->new; $try->call(sub { my (@args) = @_; return [@args]; }); my $result = $try->result(1..5); # [1..5]
package main; use Venus::Try; my $try = Venus::Try->new; $try->call(sub {die}); my $result = $try->result; # Exception! Venus::Error
To install Venus, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Venus
CPAN shell
perl -MCPAN -e shell install Venus
For more information on module installation, please visit the detailed CPAN module installation guide.