.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Lexical::Failure 3pm" .TH Lexical::Failure 3pm "2023-08-01" "perl v5.36.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Lexical::Failure \- User\-selectable lexically\-scoped failure signaling .SH "VERSION" .IX Header "VERSION" This document describes Lexical::Failure version 0.001001 .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& package Your::Module; \& \& # Set up this module for lexical failure handling... \& use Lexical::Failure; \& \& # Each time module is imported, set up failure handler... \& sub import { \& my ($package, %named_arg) = @_; \& \& ON_FAILURE( $named_arg{\*(Aqfail\*(Aq} ); \& } \& \& # Then, in the module\*(Aqs subs/methods, call fail() to fail... \& sub inverse_square { \& my ($n) = @_; \& \& if ($n == 0) { \& fail "Can\*(Aqt invert zero"; \& } \& \& return 1/$n**2; \& } \& \& sub load_file { \& my ($filename) = @_; \& \& fail \*(AqNo such file: \*(Aq, $filename \& if ! \-r $filename; \& \& local (@ARGV, $/) = $filename; \& return readline; \& } .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" This module sets up two new keywords: \f(CW\*(C`fail\*(C'\fR and \f(CW\*(C`ON_FAILURE\*(C'\fR, with which you can quickly create modules whose failure signaling is lexicially scoped, under the control of client code. .PP Normally, modules specify some fixed mechanism for error handling and require client code to adapt to that policy. One module may signal errors by returning \f(CW\*(C`undef\*(C'\fR, or perhaps some special \*(L"error object\*(R". Another may \f(CW\*(C`die\*(C'\fR or \f(CW\*(C`croak\*(C'\fR on failure. A third may set a flag variable. A fourth may require the client code to set up a callback, which is executed on failure. .PP If you are using all four modules, your own code now has to check for failure in four different ways, depending on where the failing component originated. If you would rather that \fIall\fR components throw exceptions, or all return \f(CW\*(C`undef\*(C'\fR, you will probably have to write wrappers around 3/4 of them, to convert from their \*(L"native\*(R" failure mechanism to your preferred one. .PP Lexical::Failure offers an alternative: a simple mechanism with which module authors can generically specify \*(L"fail here with this message\*(R" (using the \f(CW\*(C`fail\*(C'\fR keyword), but then allow each block of client code to decide how that failure is reported to it within its own lexical scope (using the \f(CW\*(C`ON_FAILURE\*(C'\fR keyword). .PP Module authors can still provide a default failure signaling mechanism, for when client code does not specify how errors are to be reported. This is handy for ensuring backwards compatibility in existing modules that are converted to this new failure signaling approach. .SH "INTERFACE" .IX Header "INTERFACE" .SS "Accessing the \s-1API\s0" .IX Subsection "Accessing the API" To install the new \f(CW\*(C`fail\*(C'\fR and \f(CW\*(C`ON_FAILURE\*(C'\fR keywords, simple load the module: .PP .Vb 1 \& use Lexical::Failure; .Ve .PP \fIChanging the names of the \s-1API\s0 keywords\fR .IX Subsection "Changing the names of the API keywords" .PP To avoid name conflicts, you can change the name of either (or both) of the keywords that the module sets up, by passing a named argument when loading the module. The name of the argument should be the standard name of the keyword you want to rename, and the value of the argument should be a string containing the new name. For example: .PP .Vb 4 \& use Lexical::Failure ( \& fail => \*(Aqreturn_error\*(Aq, \& ON_FAILURE => \*(Aqset_error_handler\*(Aq, \& ); \& \& sub import { \& my ($package, %named_arg) = @_; \& \& set_error_handler( $named_arg{\*(Aqfail\*(Aq} ); \& } \& \& sub inverse_square { \& my ($n) = @_; \& \& return_error "Can\*(Aqt invert zero" if $n == 0; \& \& return 1/$n**2; \& } .Ve .ie n .SS "Signaling failure with ""fail""" .el .SS "Signaling failure with \f(CWfail\fP" .IX Subsection "Signaling failure with fail" Once the module is loaded, you simply use the \f(CW\*(C`fail\*(C'\fR keyword in place of \f(CW\*(C`return\*(C'\fR, \f(CW\*(C`return undef\*(C'\fR, \f(CW\*(C`die\*(C'\fR, \f(CW\*(C`croak\*(C'\fR, \f(CW\*(C`confess\*(C'\fR, or any other mechanism by which you would normally indicate failure. .PP You can call \f(CW\*(C`fail\*(C'\fR with any number of arguments, including none, and these will be passed to whichever failure handler the client code eventually selects (see below). .PP Note that \f(CW\*(C`fail\*(C'\fR is a keyword, not a subroutine (that is, it's like \&\f(CW\*(C`return\*(C'\fR itself, and not something you can call as part of a larger expression). .PP Note too that, as failure handlers operate at the interface level of a module, calling \f(CW\*(C`fail\*(C'\fR in a nested call within the package signals the failure from the package boundary (i.e. like \f(CW\*(C`croak\*(C'\fR and \f(CW\*(C`carp\*(C'\fR do). .ie n .SS "Specifying a lexically scoped failure handler with ""ON_FAILURE""" .el .SS "Specifying a lexically scoped failure handler with \f(CWON_FAILURE\fP" .IX Subsection "Specifying a lexically scoped failure handler with ON_FAILURE" You set up a failure-signaling interface for client code by placing the \&\f(CW\*(C`ON_FAILURE\*(C'\fR keyword in your module's \f(CW\*(C`import()\*(C'\fR subroutine (or in a subroutine called from your \f(CW\*(C`import()\*(C'\fR). .PP The keyword expects one argument, which specifies how failures in the module are to be handled in the lexical scope where your module was loaded. The single argument can be: .IP "\(bu" 4 a string containing the name of a named failure handler .IP "\(bu" 4 a reference to a variable, into which failure signals will be stored .IP "\(bu" 4 a reference to a subroutine, which will be used as a callback and invoked whenever a failure is to be signalled .IP "\(bu" 4 \&\f(CW\*(C`undef\*(C'\fR (or no argument at all), in which case \f(CW\*(C`ON_FAILURE\*(C'\fR does nothing. This means you don't need to bother checking whether a failure specifier was passed in to your \f(CW\*(C`import()\*(C'\fR. Just pass in the resulting \f(CW\*(C`undef\*(C'\fR value...and it's ignored. .PP Typically, then, you have your \f(CW\*(C`import()\*(C'\fR subroutine accept an argument through which client code indicates its desired failure mode: .PP .Vb 1 \& package Your::Module; \& \& sub import { \& my ($package, %named_arg) = @_; \& \& ON_FAILURE $named_arg{\*(Aqfail\*(Aq}; \& } .Ve .PP Then the client code can specify different reporting strategies in different lexical scopes: .PP .Vb 2 \& # Hereafter, report failures by returning undef... \& use Your::Module fail => \*(Aqundef\*(Aq; \& \& { \& # But in this block, make errors fatal... \& use Your::Module fail => \*(Aqcroak\*(Aq; \& \& { \& # And in here, set a flag... \& my $nested_error_flag; \& use Your::Module fail => \e$nested_error_flag; \& \& { \& # And in here, any error is quietly loggged... \& use Your::Module fail => sub { $logger\->error(@_) }; \& } \& } \& \& # Back to croaking errors here \& } \& \& # Back to returning undef here .Ve .PP Each \f(CW\*(C`use Your::Module\*(C'\fR invokes \f(CW\*(C`Your::Module::import()\*(C'\fR, whereupon the call to \f(CW\*(C`ON_FAILURE\*(C'\fR installs the specified failure handler into the lexical scope in which \f(CW\*(C`use Your::Module\*(C'\fR occurred. The installed handler is specific to Your::Module, so if two or more modules are each using Lexical::Failure, client code can set failure-signaling policies for each module independently in the same scope. .PP \fINamed failure handlers\fR .IX Subsection "Named failure handlers" .PP If \f(CW\*(C`ON_FAILURE\*(C'\fR is passed a string, that string is treated as the name of a predefined failure handler. Lexical::Failure provides six standard named handlers: .ie n .IP """ON_FAILURE \*(Aqnull\*(Aq""" 4 .el .IP "\f(CWON_FAILURE \*(Aqnull\*(Aq\fR" 4 .IX Item "ON_FAILURE null" Specifies that each \f(CW\*(C`fail @args\*(C'\fR should act like: .Sp .Vb 1 \& return; .Ve .Sp That is: return \f(CW\*(C`undef\*(C'\fR in scalar context or return an empty list in list context. .Sp Note that, this context-sensitive behaviour can occasionally lead to subtle errors. For example, if these three subroutines are using \&\f(CW\*(C`ON_FAILURE \*(Aqnull\*(Aq\*(C'\fR failure signaling: .Sp .Vb 5 \& my %personal_data = ( \& name => get_name(), \& age => get_age(), \& status => get_status(), \& ); .Ve .Sp then if any of them fails, it will return an empty list, messing up the initialization of the hash. In such cases, \f(CW\*(C`ON_FAILURE \*(Aqundef\*(Aq\*(C'\fR is a better alternative. .ie n .IP """ON_FAILURE \*(Aqundef\*(Aq""" 4 .el .IP "\f(CWON_FAILURE \*(Aqundef\*(Aq\fR" 4 .IX Item "ON_FAILURE undef" Specifies that each \f(CW\*(C`fail @args\*(C'\fR should act like: .Sp .Vb 1 \& return undef; .Ve .Sp Note that to get this behaviour, the argument needs to be \f(CW\*(Aqundef\*(Aq\fR (a five letter string), not \f(CW\*(C`undef\*(C'\fR (the special undefined value). .Sp Note too that, when this handler is selected, \f(CW\*(C`fail\*(C'\fR returns an \&\f(CW\*(C`undef\*(C'\fR even in list context. This can be problematical, as an \f(CW\*(C`undef\*(C'\fR is (to many people's surprise) \fItrue\fR in list context. For example, if \&\f(CW\*(C`get_results()\*(C'\fR returns \f(CW\*(C`undef\*(C'\fR on failure, the conditional test of this \f(CW\*(C`if\*(C'\fR will still be true: .Sp .Vb 3 \& if (my @results = get_results($data)) { \& .... \& } .Ve .Sp because \f(CW@results\fR will then contain one element (the \f(CW\*(C`undef\*(C'\fR), and a non-empty array always evaluates true in boolean context. .Sp For this reason it's usually better to use \f(CW\*(C`ON_FAILURE \*(Aqnull\*(Aq\*(C'\fR instead. .ie n .IP """ON_FAILURE \*(Aqdie\*(Aq""" 4 .el .IP "\f(CWON_FAILURE \*(Aqdie\*(Aq\fR" 4 .IX Item "ON_FAILURE die" Specifies that each \f(CW\*(C`fail @args\*(C'\fR should act like: .Sp .Vb 1 \& die @args; .Ve .ie n .IP """ON_FAILURE \*(Aqcroak\*(Aq""" 4 .el .IP "\f(CWON_FAILURE \*(Aqcroak\*(Aq\fR" 4 .IX Item "ON_FAILURE croak" Specifies that each \f(CW\*(C`fail @args\*(C'\fR should act like: .Sp .Vb 1 \& Carp::croak(@args); .Ve .ie n .IP """ON_FAILURE \*(Aqconfess\*(Aq""" 4 .el .IP "\f(CWON_FAILURE \*(Aqconfess\*(Aq\fR" 4 .IX Item "ON_FAILURE confess" Specifies that each \f(CW\*(C`fail @args\*(C'\fR should act like: .Sp .Vb 1 \& Carp::confess(@args); .Ve .ie n .IP """ON_FAILURE \*(Aqfailobj\*(Aq""" 4 .el .IP "\f(CWON_FAILURE \*(Aqfailobj\*(Aq\fR" 4 .IX Item "ON_FAILURE failobj" Specifies that each \f(CW\*(C`fail @args\*(C'\fR should act like: .Sp .Vb 4 \& return Lexical::Failure::Objects\->new( \& msg => ( @args == 1 ? $args[0] : "@args" ), \& context => [caller 1] \& ); .Ve .Sp In other words, \f(CW\*(C`ON_FAILURE \*(Aqfailobj\*(Aq\*(C'\fR causes \f(CW\*(C`fail\*(C'\fR to return a special object encapsulating the arguments passed to \f(CW\*(C`fail\*(C'\fR and the call context in which the \f(CW\*(C`fail\*(C'\fR occurred. .Sp See the documentation of Lexical::Failure::Objects for more details on this alternative. .PP You can also set up other named failure handlers of your own devising (see \*(L"Specifying additional named failure handlers\*(R"). .PP \fIVariables as failure handlers\fR .IX Subsection "Variables as failure handlers" .PP If \f(CW\*(C`ON_FAILURE\*(C'\fR is passed a reference to a scalar, array, or hash, that variable becomes the \*(L"receiver\*(R" of subsequent failure reports, as follows: .ie n .IP """ON_FAILURE \e$scalar""" 4 .el .IP "\f(CWON_FAILURE \e$scalar\fR" 4 .IX Item "ON_FAILURE $scalar" Specifies that \f(CW\*(C`fail @args\*(C'\fR should act like: .Sp .Vb 2 \& $scalar = [@args]; \& return undef; .Ve .ie n .IP """ON_FAILURE \e@array""" 4 .el .IP "\f(CWON_FAILURE \e@array\fR" 4 .IX Item "ON_FAILURE @array" Specifies that \f(CW\*(C`fail @args\*(C'\fR should act like: .Sp .Vb 2 \& push @array, [@args]; \& return undef; .Ve .ie n .IP """ON_FAILURE \e%hash""" 4 .el .IP "\f(CWON_FAILURE \e%hash\fR" 4 .IX Item "ON_FAILURE %hash" Specifies that \f(CW\*(C`fail @args\*(C'\fR should act like: .Sp .Vb 2 \& $hash{ $CURRENT_SUBNAME } = [@args]; \& return undef; .Ve .PP \fISubroutines as failure handlers\fR .IX Subsection "Subroutines as failure handlers" .PP \&\f(CW\*(C`ON_FAILURE\*(C'\fR can also be passed a reference to a subroutine, which then acts like a callback when failures are signalled. .PP In other words: .PP .Vb 1 \& ON_FAILURE $subroutine_ref; .Ve .PP causes \f(CW\*(C`fail @args\*(C'\fR to act like: .PP .Vb 1 \& return $subroutine_ref\->(@args); .Ve .PP The availability of this alternative means that client code can create entirely new failure-signaling behaviours whenever needed. For example: .PP .Vb 2 \& # Signal failure by logging an error and returning negatively... \& use Your::Module fail => sub { $logger\->error(@_); return \-1; }; \& \& \& # Signal failure by returning undef/empty list, \& # except in one critical case... \& use Your::Module fail => sub { \& my $msg = "@_"; \& croak $msg if $msg =~ /dangerous/; \& return; \& }; \& \& \& # The very first failure is instantly (and unluckily) fatal... \& use Your::Module fail => sub { carp(@_); exit(13) }; .Ve .PP \fIRestricting how client code can signal failure\fR .IX Subsection "Restricting how client code can signal failure" .PP Because the call to \f(CW\*(C`ON_FAILURE\*(C'\fR must occur in your module's \&\f(CW\*(C`import()\*(C'\fR subroutine, you always have ultimate control over what types of failure signaling the client code may request from your module. .PP For example, to prevent client code from requesting \f(CW\*(C`return undef\*(C'\fR behaviours: .PP .Vb 2 \& sub import { \& my ($package, %named_arg) = @_; \& \& croak "Can\*(Aqt specify \*(Aqundef\*(Aq as a failure handler" \& if $named_arg{\*(Aqfail\*(Aq} eq \*(Aqundef\*(Aq; \& \& ON_FAILURE $named_arg{\*(Aqfail\*(Aq}; \& } .Ve .PP or to quietly convert 'die' behaviours into (much more useful) 'croak' behaviours: .PP .Vb 2 \& sub import { \& my ($package, %named_arg) = @_; \& \& $named_arg{\*(Aqfail\*(Aq} =~ s/^die$/croak/; \& \& ON_FAILURE $named_arg{\*(Aqfail\*(Aq}; \& } .Ve .PP \fISpecifying a module's default failure handler\fR .IX Subsection "Specifying a module's default failure handler" .PP In any scope where no explicit failure signaling behaviour has been specified, Lexical::Failure defaults to its standard \&\f(CW\*(Aqcroak\*(Aq\fR behaviour (see \*(L"Named failure handlers\*(R"). .PP However, you can also specify a different default for your module, by adding a named argument when you load Lexical::Failure: .PP .Vb 2 \& # Default to full confession on failure... \& use Lexical::Failure default => \*(Aqconfess\*(Aq; \& \& # Default to \*(Aqreturn undef or empty list\*(Aq on failure... \& use Lexical::Failure default => \*(Aqnull\*(Aq; \& \& # Default to instant fatality on failure... \& use Lexical::Failure default => sub { carp(@_); exit() }; .Ve .PP The values allowed for the \f(CW\*(Aqdefault\*(Aq\fR option are somewhat more restrictive than those which can be passed directly to \f(CW\*(C`ON_FAILURE\*(C'\fR; you can specify only standard named handlers (see \*(L"Named failure handlers\*(R") or a subroutine reference. .PP If you need your default to be a non-standard named handler (see \&\*(L"Specifying additional named failure handlers\*(R") or a reference to a variable, you must arrange that in your \f(CW\*(C`import()\*(C'\fR instead. For example: .PP .Vb 2 \& sub import { \& my ($package, %named_arg) = @_; \& \& # Install failure signaling, if specified... \& if (defined $named_arg{\*(Aqfail\*(Aq}) { \& ON_FAILURE $named_arg{\*(Aqfail\*(Aq}; \& } \& \& # Otherwise, default to pushing errors onto a package variable \& # (yeah, this is a HORRIBLE idea, but it\*(Aqs what our boss decided!) \& else { \& ON_FAILURE \e@Your::Module::errors; \& } \& } .Ve .PP \fISpecifying additional named failure handlers\fR .IX Subsection "Specifying additional named failure handlers" .PP The six standard named failure handlers provide convenient declarative shortcuts for client code. That is, instead of constantly having to create messy subroutines like: .PP .Vb 7 \& use Your::Module \& fail => sub { \& return Lexical::Failure::Objects\->new( \& msg => (@_ == 1 ? $_[0] : "@_"), \& context => [caller 1], \& ); \& }; .Ve .PP client code can just request: .PP .Vb 1 \& use Your::Module fail => \*(Aqfailobj\*(Aq; .Ve .PP However, you may wish to offer a similar declarative interface for other failure-signaling behaviours that your client code is likely to need. For example: .PP .Vb 1 \& use Your::Module fail => \*(Aqlogged\*(Aq; \& \& use Your::Module fail => \*(Aqexit\*(Aq; \& \& use Your::Module fail => \*(Aqloud undef\*(Aq; .Ve .PP Lexical::Failure provides a simple way to set up extra named handlers like these. You just specify the name and associated callback for each when loading the module: .PP .Vb 1 \& package Your::Module; \& \& use Lexical::Failure handlers => { \& \*(Aqlogged\*(Aq => sub { $logger\->error(@_); }, \& \*(Aqexit\*(Aq => sub { say @_; exit; }, \& \*(Aqloud undef\*(Aq => sub { carp(@_); return undef; }, \& }; .Ve .PP The \f(CW\*(Aqhandlers\*(Aq\fR option expects a reference to a hash, in which each key is the name of a new named failure handler, and each corresponding value is a reference to a subroutine implementing the behaviour of that named handler. .PP Once specified, any of the new handler names may be passed to \&\f(CW\*(C`ON_FAILURE\*(C'\fR to specify that \f(CW\*(C`fail\*(C'\fR should use the corresponding callback to signal failures. .PP Note that any extra named handlers defined in this way are only available from the module in which they are defined. .SH "DIAGNOSTICS" .IX Header "DIAGNOSTICS" .ie n .IP """Unknown failure handler: %s""" 4 .el .IP "\f(CWUnknown failure handler: %s\fR" 4 .IX Item "Unknown failure handler: %s" You called \f(CW\*(C`ON_FAILURE\*(C'\fR with a string as the handler specification. However, that string was not one of the standard named handlers (\f(CW\*(Aqconfess\*(Aq\fR, \f(CW\*(Aqcroak\*(Aq\fR, \f(CW\*(Aqdie\*(Aq\fR, \f(CW\*(Aqfailobj\*(Aq\fR, \f(CW\*(Aqundef\*(Aq\fR, or \f(CW\*(Aqnull\*(Aq\fR), nor any of the extra handlers you may have specified with a \f(CW\*(Aqhandlers\*(Aq\fR option when loading Lexical::Failure. .Sp Did you perhaps misspell the handler name? .ie n .IP """Unknown default failure handler: %s""" 4 .el .IP "\f(CWUnknown default failure handler: %s\fR" 4 .IX Item "Unknown default failure handler: %s" When loading Lexical::Failure, you specified a default handler for all scopes like so: .Sp .Vb 1 \& use Lexical::Failure default => \*(AqSOME_STRING\*(Aq; .Ve .Sp However, the string you specified did not match the name of any of the standard handlers (\f(CW\*(Aqconfess\*(Aq\fR, \f(CW\*(Aqcroak\*(Aq\fR, \f(CW\*(Aqdie\*(Aq\fR, \f(CW\*(Aqfailobj\*(Aq\fR, \&\f(CW\*(Aqundef\*(Aq\fR, or \f(CW\*(Aqnull\*(Aq\fR) nor the name of any handler you had specified yourself using the \f(CW\*(Aqhandlers\*(Aq\fR option. .Sp Did you perhaps misspell the handler name? .ie n .IP """Can\*(Aqt call ON_FAILURE after compilation""" 4 .el .IP "\f(CWCan\*(Aqt call ON_FAILURE after compilation\fR" 4 .IX Item "Cant call ON_FAILURE after compilation" Lexical failure handlers must be specified at compile-time (usually in your module's \f(CW\*(C`import()\*(C'\fR subroutine). However, you called \f(CW\*(C`ON_FAILURE\*(C'\fR at runtime. .Sp Move the call into your module's \f(CW\*(C`import()\*(C'\fR, or into some other subroutine that \f(CW\*(C`import()\*(C'\fR calls. .ie n .IP """Can\*(Aqt call ON_FAILURE outside a subroutine""" 4 .el .IP "\f(CWCan\*(Aqt call ON_FAILURE outside a subroutine\fR" 4 .IX Item "Cant call ON_FAILURE outside a subroutine" You probably attempted to set up a lexical handler at the top level of your module's source code. For example: .Sp .Vb 1 \& package Your::Module; \& \& use Lexical::Failure; \& ON_FAILURE(\*(Aqdie\*(Aq); .Ve .Sp The lexical hinting mechanism that Lexical::Failure uses only works when \f(CW\*(C`ON_FAILURE\*(C'\fR is called from within your module's \&\f(CW\*(C`import()\*(C'\fR subroutine (or from a subroutine that \f(CW\*(C`import()\*(C'\fR itself calls). .Sp To achieve the \*(L"set a default handler for my module\*(R" effect intended in the previous example, rewrite it either as: .Sp .Vb 1 \& package Your::Module; \& \& use Lexical::Failure; \& sub import { ON_FAILURE(\*(Aqdie\*(Aq); } .Ve .Sp or simply: .Sp .Vb 1 \& package Your::Module; \& \& use Lexical::Failure default => \*(Aqdie\*(Aq; .Ve .ie n .IP """Missing rename for %s""" 4 .el .IP "\f(CWMissing rename for %s\fR" 4 .IX Item "Missing rename for %s" You tried to rename either \f(CW\*(C`fail\*(C'\fR or \f(CW\*(C`ON_FAILURE\*(C'\fR as part of your \&\f(CW\*(C`use Lexical::Failure\*(C'\fR call, but forgot to include the new name for the subroutine (i.e. you left out the argument expected after \f(CW\*(Aqfail\*(Aq\fR or \&\f(CW\*(AqON_FAILURE\*(Aq\fR). .ie n .IP """Missing specification for %s""" 4 .el .IP "\f(CWMissing specification for %s\fR" 4 .IX Item "Missing specification for %s" You tried to specify either the \f(CW\*(Aqdefault\*(Aq\fR or \f(CW\*(Aqhandlers\*(Aq\fR option as part of your \f(CW\*(C`use Lexical::Failure\*(C'\fR call, but forgot to include the corresponding default value or handlers hash (i.e. you left out the argument expected after \f(CW\*(Aqdefault\*(Aq\fR or \f(CW\*(Aqhandlers\*(Aq\fR). .ie n .IP """Value for %s option must be a %s""" 4 .el .IP "\f(CWValue for %s option must be a %s\fR" 4 .IX Item "Value for %s option must be a %s" You passed a \fIkeyword\fR \f(CW\*(C`=>\*(C'\fR \fIvalue\fR pair to \f(CW\*(C`use Lexical::Failure\*(C'\fR, but the value was of the wrong type for that particular keyword. See \&\*(L"Changing the names of the \s-1API\s0 keywords\*(R" or \*(L"Specifying a module's default failure handler\*(R" or \*(L"Specifying additional named failure handlers\*(R" for the correct usage. .ie n .IP """Handlers in \*(Aqhandlers\*(Aq hash must all be code references""" 4 .el .IP "\f(CWHandlers in \*(Aqhandlers\*(Aq hash must all be code references\fR" 4 .IX Item "Handlers in handlers hash must all be code references" The \f(CW\*(Aqhandlers\*(Aq\fR option to \f(CW\*(C`use Lexical::Failure\*(C'\fR expects a reference to a hash in which each value is a code reference. At least one of the values in the hash you passed was something else. .ie n .IP """Unexpected argument (%s)""" 4 .el .IP "\f(CWUnexpected argument (%s)\fR" 4 .IX Item "Unexpected argument (%s)" \&\f(CW\*(C`use Lexical::Failure\*(C'\fR accepts only four arguments: .Sp .Vb 6 \& use Lexical::Failure ( \& fail => $NEW_NAME, \& ON_FAILURE => $NEW_NAME, \& default => $HANDLER_NAME, \& handlers => \e%HANDLER_HASH, \& ); .Ve .Sp You attempted to pass it something else. Or perhaps you misspelled one of the above keywords? .ie n .IP """Invalid handler type (%s) in call to ON_FAILURE""" 4 .el .IP "\f(CWInvalid handler type (%s) in call to ON_FAILURE\fR" 4 .IX Item "Invalid handler type (%s) in call to ON_FAILURE" The argument passed to \f(CW\*(C`ON_FAILURE\*(C'\fR must be either a string (i.e. the name of a named handler) or a reference to a subroutine (i.e. the handler itself) or a reference to a variable (i.e. the lvalue into which error messages are to be assigned). .Sp You passed it something else (probably a regex or a reference to a reference). .SH "CONFIGURATION AND ENVIRONMENT" .IX Header "CONFIGURATION AND ENVIRONMENT" Lexical::Failure requires no configuration files or environment variables. .SH "DEPENDENCIES" .IX Header "DEPENDENCIES" Requires the modules: Scope::Upper, Keyword::Simple, and Test::Effects. .PP Also requires the Lexical::Failure::Objects helper module included in its distribution. .SH "INCOMPATIBILITIES" .IX Header "INCOMPATIBILITIES" None reported. .SH "BUGS AND LIMITATIONS" .IX Header "BUGS AND LIMITATIONS" No bugs have been reported. .PP Please report any bugs or feature requests to \&\f(CW\*(C`bug\-lexical\-failure@rt.cpan.org\*(C'\fR, or through the web interface at . .SH "AUTHOR" .IX Header "AUTHOR" Damian Conway \f(CW\*(C`\*(C'\fR .SH "LICENCE AND COPYRIGHT" .IX Header "LICENCE AND COPYRIGHT" Copyright (c) 2013, Damian Conway \f(CW\*(C`\*(C'\fR. All rights reserved. .PP This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic. .SH "DISCLAIMER OF WARRANTY" .IX Header "DISCLAIMER OF WARRANTY" \&\s-1BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE \*(L"AS IS\*(R" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.\s0 .PP \&\s-1IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE\s0 (\s-1INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE\s0), \s-1EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.\s0