Lexical::Failure::Objects(3) Special failure objects for Lexical::Failure

VERSION

This document describes Lexical::Failure::Objects version 0.000001

DESCRIPTION

This module implements the ``failure objects'' returned by the optional 'failobj' mechanism of the "Lexical::Failure" module.

When "ON_FAILURE 'failobj'" is in effect, any call to "fail" will return one of these objects, which simulates a special out-of-band value that you can either explicitly test for failure or else simply ignore and automatically get an exception.

For example, given the subroutine:

    package Math;
    use Lexical::Failure;
    sub inverse_square {
        my ($n) = @_;
        if ($n == 0) {
            fail "Can't invert zero";
        }
        return 1/$n**2;
    }

when 'failobj' is the selected failure signalling strategy:

    use Math (fail => 'failobj')

then failure can either be tested for explicitly:

    # This block skipped if $n == 0...
    if (my $inv_sq = Math::inverse_square($n) {
        print $inv_sq;
    }

or else simply ignored, in which case an exception will automatically be thrown:

    print inverse_square($n);    # ...throw exception if $n == 0

INTERFACE

If it is used as a boolean, a failure object evaluates false (i.e. it acts as if "ON_FAILURE 'undef'" had been in effect).

If it is used as a value in any other way (as a string, as a reference, as a regex, as a filehandle, etc., etc.), or if it's ignored and allowed to go out of scope without being evaluated at all, then a failure object throws an exception (i.e. it acts as if "ON_FAILURE 'croak'" had been in effect).

Constructor (new())

The class's constructor expects two named arguments:

    $failure_obj = Lexical::Failure::Objects->new(
                       msg     => $MESSAGE_STR_OR_OBJ,
                       context => [$PACKAGE, $FILE, $LINE, $SUBNAME],
                   );

You should never normally need to construct failure objects directly; it's better to let "Lexical::Failure" craete them automatically via its 'failobj' mechanism.

Methods

"Lexical::Failure::Objects" also provides four methods with which you can query the location of the failure that they represent. None of these methods takes any arguments.
"$failobj->subname()"
Returns the name of the subroutine in which the failure was signaled. That is, the equivalent of "(caller 0)[3]".
"$failobj->file()"
Returns the name of the file containing the subroutine call from which failure was signaled. That is, the equivalent of "(caller 0)[1]".
"$failobj->line()"
Returns the line number of the subroutine call from which failure was signaled. That is, the equivalent of "(caller 0)[2]".
"$failobj->context()"
Returns a string summarizing the information provided by the previous three methods, in the form:

    "call to <subname> at <file> line <line>"

DIAGNOSTICS

None of their own.

If they throw an exception (when misused or ignored), it will be the exception that "fail" would otherwise have thrown.

CONFIGURATION AND ENVIRONMENT

Lexical::Failure::Objects requires no configuration files or environment variables.

DEPENDENCIES

Requires the Hash::Util::FieldHash module.

INCOMPATIBILITIES

None reported.

BUGS AND LIMITATIONS

No bugs have been reported.

Please report any bugs or feature requests to "[email protected]", or through the web interface at <http://rt.cpan.org>.

AUTHOR

Damian Conway "<[email protected]>"

LICENCE AND COPYRIGHT

Copyright (c) 2013, Damian Conway "<[email protected]>". All rights reserved.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic.

DISCLAIMER OF WARRANTY

BECAUSE 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 ``AS IS'' 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.

IN 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 (INCLUDING 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), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.