NAME
Role::Singleton - Add a singleton constructor to your class
VERSION
This document describes Role::Singleton version 0.2
SYNOPSIS
Object:
package ZeroCool
## no critic (RequireUseStrict) - Moo does strict
use Moo;
with 'Role::Singleton';
…
Code:
use ZeroCool;
my $z3r0 = ZeroCool->new(…); # returns a new object every time, not a singlteon (see Role::Singleton::New if you want to do that)
my $z3r1 = ZeroCool->singleton(…); # returns the object from the initial call to singleton() everytime, regardless of args in the subsequent calls
my $z3r2 = ZeroCool->instance(…); # alias to singleton()
DESCRIPTION
See http://en.wikipedia.org/wiki/Singleton_pattern for info about singletons.
INTERFACE
It adds these methods:
singleton()
Creates a new object initialized with the arguments provided and then returns it.
Subsequent calls to singleton() will return the singleton instance ignoring any arguments. NOTE: This is different from MooseX::Singleton which does not allow any arguments.
If you’d rather have new() be a singleton use Role::Singleton::New instead.
instance()
Alias to singleton(). This allows for compatibility with other constructor roles (e.g. MooseX::Singleton, Moox::Singleton).
It is nice because it allows you to switch between what type of instance you get without having to update your code.
For example, if your code does ZeroCool->instance() a million times and you see the need to change from using a singleton to a multiton (or some other instance() defining role), all you have to do is change your class and the consumers will not need to do anything besides update their ZeroCool install (i.e. not update their code in a million places).
Of course, there are also cases when being explicit is important so you’d use singleton().
You can also have both singleton and multiple support (the first one that is with()d will defined instance()):
This will give you multiton(), singleton(), and instance() is a singleton:
with 'Role::Singleton';
with 'Role::Multiton';
This will give you multiton(), singleton(), and instance() is a multiton:
with 'Role::Multiton';
with 'Role::Singleton';
If you’d rather have new() be a singleton use Role::Singleton::New instead.
DIAGNOSTICS
Throws no warnings or errors of its own.
CONFIGURATION AND ENVIRONMENT
Role::Singleton requires no configuration files or environment variables.
DEPENDENCIES
SEE ALSO
Role::Singleton::New, Role::Multiton, Role::Multiton::New
INCOMPATIBILITIES
None reported.
See "INCOMPATIBILITIES" in Role::Multiton for info about object system specific compatibility.
BUGS AND LIMITATIONS
No bugs have been reported.
Please report any bugs or feature requests to bug-role-multiton@rt.cpan.org
, or through the web interface at http://rt.cpan.org.
AUTHOR
Daniel Muey <http://drmuey.com/cpan_contact.pl>
LICENCE AND COPYRIGHT
Copyright (c) 2013, Daniel Muey <http://drmuey.com/cpan_contact.pl>
. 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.