|
NAMEData::Object::TryABSTRACTTry Class for Perl 5SYNOPSISuse 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); DESCRIPTIONThis package provides an object-oriented interface for performing complex try/catch operations.ATTRIBUTESThis package has the following attributes:argumentsarguments(ArrayRef) This attribute is read-only, accepts "(ArrayRef)" values, and is optional. invocantinvocant(Object) This attribute is read-only, accepts "(Object)" values, and is optional. on_catchon_catch(ArrayRef[CodeRef]) This attribute is read-write, accepts "(ArrayRef[CodeRef])" values, and is optional. 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. on_tryon_try(CodeRef) This attribute is read-write, accepts "(CodeRef)" values, and is optional. METHODSThis package implements the following methods:callcall(Str | CodeRef $arg) : 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.
callbackcallback(Str | CodeRef $arg) : 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.
catchcatch(Str $isa, Str | CodeRef $arg) : 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.
defaultdefault(Str | CodeRef $arg) : Object The default method takes a method name or coderef and is triggered if no "catch" conditions match the exception thrown.
executeexecute(CodeRef $arg, 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.
finallyfinally(Str | CodeRef $arg) : 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.
maybemaybe() : Object The maybe method registers a default "catch" condition that returns falsy, i.e. an empty string, if an exception is encountered.
no_catchno_catch() : Object The no_catch method removes any configured catch conditions and returns the object.
no_defaultno_default() : Object The no_default method removes any configured default condition and returns the object.
no_finallyno_finally() : Object The no_finally method removes any configured finally condition and returns the object.
no_tryno_try() : Object The no_try method removes any configured "try" operation and returns the object.
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.
AUTHORAl Newkirk, "awncorp@cpan.org"LICENSECopyright (C) 2011-2019, Al Newkirk, et al.This is free software; you can redistribute it and/or modify it under the terms of the The Apache License, Version 2.0, as elucidated in the "license file" <https://github.com/iamalnewkirk/data-object-try/blob/master/LICENSE>. PROJECTWiki <https://github.com/iamalnewkirk/data-object-try/wiki>Project <https://github.com/iamalnewkirk/data-object-try> Initiatives <https://github.com/iamalnewkirk/data-object-try/projects> Milestones <https://github.com/iamalnewkirk/data-object-try/milestones> Contributing <https://github.com/iamalnewkirk/data-object-try/blob/master/CONTRIBUTE.md> Issues <https://github.com/iamalnewkirk/data-object-try/issues>
Visit the GSP FreeBSD Man Page Interface. |