NAME

MooX::Failover - Instantiate Moo classes with failover

VERSION

v0.1.0_01

SYNOPSIS

# In your class:

package MyClass;

use Moo;

has 'attr' => ( ... );

with 'MooX::Failover'; # use *after* attributes are declared

# When using the class

my $obj = MyClass->new( %args, failover_to => 'OtherClass' );

# If %args contains missing or invalid values or new otherwise
# fails, then $obj will be of type "OtherClass".

INSTALLATION

See How to install CPAN modules.

Required Modules

This distribution requires Perl v5.10.0.

This distribution requires the following modules:

This distribution recommends the following modules:

RECENT CHANGES

  • An initial port of MooseX-Failover to Moo.

See the Changes file for a longer revision history.

DESCRIPTION

This role provides constructor failover for Moo classes.

If a class cannot be instantiated because of invalid arguments (perhaps from an untrusted source), then instead it returns the failover class (passing the same arguments to that class).

This allows for cleaner design, by not forcing you to duplicate type checking for class parameters.

Note that this is roughly equivalent to using

my $obj = eval { MyClass->new(%args) //
   OtherClass->new( %args, error => $@ );

Note that your failover class should support the same methods as the original class. A use case for this role would be for instantiating Web::Machine::Resource objects, where the failover is a Web::Machine::Resource object that returns an error page.

Ideally, your failover class would satisy the Liskov Substitution Principle, so that (roughly) all provable properties of the original class are also provable of the failover class. In practice, we only care about the properties (methods and attributes) that are actually used in our programs.

CAVEATS

This module is experimental. It works, but the current version is significantly slower than using a simple eval or try block. So it's not recommended for production code.

SEE ALSO

MooseX::Failover

AUTHOR

Robert Rothenberg <rrwo@thermeon.com>

Acknowledgements

Thermeon Europe.
Piers Cawley.
Graham Knop.

COPYRIGHT

Copyright 2014 Thermeon Europe.

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

This program is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose.