Name

Unexpected::Functions - A collection of functions used in this distribution

Synopsis

package YourApp::Exception;

use Moo;

extends 'Unexpected';
with    'Unexpected::TraitFor::ExceptionClasses';

package YourApp;

use Unexpected::Functions 'Unspecified';

sub EXCEPTION_CLASS { 'YourApp::Exception' }

sub throw { EXCEPTION_CLASS->throw( @_ ) }

throw Unspecified, args => [ 'parameter name' ];

Description

A collection of functions used in this distribution

Also exports any exceptions defined by the caller's EXCEPTION_CLASS as subroutines that return a subroutine that returns the subroutines name as a string. The calling package can then throw exceptions with a class attribute that takes these subroutines return values

Configuration and Environment

Defines no attributes

Subroutines/Methods

catch_class

use Try::Tiny;

try         { die $exception_object }
catch_class [ 'exception_class' => sub { # handle exception }, ... ],
finally     { # always do this };

See Try::Tiny::ByClass. Checks the exception object's class attribute against the list of exception class names passed to catch_class. If there is a match, call the subroutine provided to handle that exception. Re-throws the exception if there is no match or if the exception object has no class attribute

exception

$exception_object_ref = exception $optional_error;

A function which calls the caught class method

has_exception

has_exception 'exception_name' => parents => [ 'parent_exception' ],
   error => 'Error message for the exception with placeholders';

A function which calls "add_exception" in Unexpected::TraitFor::ExceptionClasses via the calling class which is assumed to inherit from a class that consumes the Unexpected::TraitFor::ExceptionClasses role

inflate_message

$message = inflate_message $template, $val1, $val2, ...;

Substitute the placeholders in the $template string, e.g. [_1], with the corresponding value. Undefined values are represented as [?], zero length strings are represented as []. Placeholder values will be quoted when substituted

inflate_placeholders

$message = inflate_placeholders $defaults, $template, $val1, $val2, ...;

Substitute the placeholders in the $template string, e.g. [_1], with the corresponding value. The $defaults argument is a tuple (array reference) containing the default substitution values for; an undefined value, a zero length value, and a boolean which if true prevents quoting of the placeholder values when they are substituted into the template

is_class_loaded

$bool = is_class_loaded $classname;

Returns true is the classname as already loaded and compiled

is_one_of_us

$bool = is_one_of_us $string_or_exception_object_ref;

Function which detects instances of this exception class

parse_arg_list

$hash_ref = parse_arg_list( <whatever> );

Coerces a hash reference from whatever arguments are passed. This function is responsible for parsing the arguments passed to the constructor. Supports the following signatures

# No defined arguments - returns and empty hash reference
Unexpected->new();

# First argument is one if our own objects - clone it
Unexpected->new( $unexpected_object_ref );

# First argument is one if our own objects, second is a hash reference
# - clone the object but mutate it using the hash reference
Unexpected->new( $unexpected_object_ref, { key => 'value', ... } );

# First argument is a code reference - the code reference returns the
# exception class and the remaining arguments are treated as a list of
# keys and values
Unexpected->new( Unspecified, args => [ 'parameter name' ] );
Unexpected->new( Unspecified, [ 'parameter name' ] ); # Shortcut

# First argmentt is a hash reference - clone it
Unexpected->new( { key => 'value', ... } );

# Only one scalar argement - the error string
Unexpected->new( $error_string );

# Second argement is a hash reference, first argument is the error
Unexpected->new( $error_string, { key => 'value', ... } );

# Odd numbered list of arguments is the error followed by keys and values
Unexpected->new( $error_string, key => 'value', ... );
Unexecpted->new( 'File [_1] not found', args => [ $filename ] );
Unexecpted->new( 'File [_1] not found', [ $filename ] ); # Shortcut

# Arguments are a list of keys and values
Unexpected->new( key => 'value', ... );

quote_bind_values

$bool = Unexpected::Functions->quote_bind_values( $bool );

Deprecated. Use third argument in "inflate_placeholders" defaults defaults. Accessor / mutator class method that toggles the state on quoting the placeholder substitution values in inflate_message. Defaults to true

throw

throw 'Path [_1] not found', args => [ 'pathname' ];

A function which calls the throw class method

throw_on_error

throw_on_error @optional_args;

A function which calls the throw_on_error class method

Diagnostics

None

Dependencies

Exporter::Tiny
Package::Stash
Sub::Install

Incompatibilities

There are no known incompatibilities in this module

Bugs and Limitations

There are no known bugs in this module. Please report problems to the address below. Patches are welcome

Acknowledgements

Larry Wall - For the Perl programming language

Author

Peter Flanigan, <pjfl@cpan.org>

License and Copyright

Copyright (c) 2017 Peter Flanigan. All rights reserved

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

This program is distributed in the hope that it will be useful, but WITHOUT WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE