|
NAMEVenus::Try - Try Class ABSTRACTTry Class for Perl 5 SYNOPSIS 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);
DESCRIPTIONThis package provides an object-oriented interface for performing complex try/catch operations. ATTRIBUTESThis package has the following attributes: invocantinvocant(Object) This attribute is read-only, accepts "(Object)" values, and is optional. argumentsarguments(ArrayRef) This attribute is read-only, accepts "(ArrayRef)" values, and is optional. on_tryon_try(CodeRef) This attribute is read-write, accepts "(CodeRef)" values, and is optional. on_catchon_catch(ArrayRef[CodeRef]) This attribute is read-write, accepts "(ArrayRef[CodeRef])" values, is optional, and defaults to "[]". on_defaulton_default(CodeRef) This attribute is read-write, accepts "(CodeRef)" values, and is optional. on_finallyon_finally(CodeRef) This attribute is read-write, accepts "(CodeRef)" values, and is optional. INHERITSThis package inherits behaviors from: Venus::Kind::Utility METHODSThis package provides the following methods: anyany() (Venus::Try) The any method registers a default "catch" condition that returns whatever value was encoutered on error and returns it as a result. Since 2.32
callcall(string | coderef $method) (Venus::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. Since 0.01
callbackcallback(string | 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. Since 0.01
catchcatch(string $isa, string | coderef $method) (Venus::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. If no callback is provided the exception is captured in a "default" operation and returned as a result. Since 0.01
defaultdefault(string | coderef $method) (Venus::Try) The default method takes a method name or coderef and is triggered if no "catch" conditions match the exception thrown. Since 0.01
errorerror(Ref $variable) (Venus::Try) The error method takes a scalar reference and assigns any uncaught exceptions to it during execution. If no variable is provided a "catch" operation will be registered to capture all Venus::Error exceptions. Since 0.01
executeexecute(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. Since 0.01
finallyfinally(string | coderef $method) (Venus::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. Since 0.01
maybemaybe() (Venus::Try) The maybe method registers a default "catch" condition that returns falsy, i.e. an undefined value, if an exception is encountered. Since 0.01
no_catchno_catch() (Venus::Try) The no_catch method removes any configured catch conditions and returns the object. Since 0.01
no_defaultno_default() (Venus::Try) The no_default method removes any configured default condition and returns the object. Since 0.01
no_finallyno_finally() (Venus::Try) The no_finally method removes any configured finally condition and returns the object. Since 0.01
no_tryno_try() (Venus::Try) The no_try method removes any configured "try" operation and returns the object. Since 0.01
resultresult(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. Since 0.01
ERRORSThis package may raise the following errors:
AUTHORSAwncorp, "awncorp@cpan.org" LICENSECopyright (C) 2022, Awncorp, "awncorp@cpan.org". This program is free software, you can redistribute it and/or modify it under the terms of the Apache license version 2.0.
|