NAME
MooX::ClassOnlyConstructor - Make Moo-based object constructors class-only methods
SYNOPSIS
In a module:
package MyApp;
use Moo;
use MooX::ClassOnlyConstructor;
Then in a script:
use MyApp;
# this works as expected
my $ma = MyApp->new();
# and this will throw an exception via croak
eval { my $ma2 = $ma->new(); };
print "$#\n";
# prints "'MyApp->new' must be called as a class method ..."
DESCRIPTION
Discussions about what calling a constuctor on an existing object should do can come close to being holy wars at times. Often, the best course of action is to simply not allow it so as to avoid the question entirely.
This module gives one the ability to prevent a Moo-based object from calling its own constructor. It does this by wrapping the constructor code generated by Moo with a simple check for $class
being a reference.
Similar behavior can be achieved with before new
but the class can not be sub-classed properly if that is done. That was the motivation for this module.
ERROR CONDITION
It is an error to use this module with a class that doesn't use Moo and an exception will be thrown in that case.
SUPPORT
Support is provided by the author. Please use the GitHub repository to report bugs or make feature requests.
https://github.com/boftx/MooX-ClassOnlyConstructor
ACKNOWLEDGEMENTS
This module draws heavily on MooX::StrictConstructor as well as much guidance and cooperation from mst
and haarg
on #moose
.
AUTHOR
Jim Bacon, <jim@nortx.com>
COPYRIGHT AND LICENSE
Copyright (C) 2014 by Jim Bacon
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.16 or, at your option, any later version of Perl 5 you may have available.