1 package MooseX::Singleton;
5 use MooseX::Singleton::Role::Object;
6 use MooseX::Singleton::Role::Meta::Class;
7 use MooseX::Singleton::Role::Meta::Instance;
10 $VERSION = eval $VERSION;
12 Moose::Exporter->setup_import_methods( also => 'Moose' );
20 my $caller = $p{for_class};
22 Moose::Util::MetaRole::apply_metaroles(
25 class => ['MooseX::Singleton::Role::Meta::Class'],
27 ['MooseX::Singleton::Role::Meta::Instance'],
29 ['MooseX::Singleton::Role::Meta::Method::Constructor'],
33 Moose::Util::MetaRole::apply_base_class_roles(
36 ['MooseX::Singleton::Role::Object'],
39 return $caller->meta();
51 MooseX::Singleton - turn your Moose class into a singleton
56 use MooseX::Singleton;
60 isa => 'HashRef[Str]',
61 default => sub { \%ENV },
66 delete MyApp->env->{PATH};
67 my $instance = MyApp->instance;
68 my $same = MyApp->instance;
72 A singleton is a class that has only one instance in an application.
73 C<MooseX::Singleton> lets you easily upgrade (or downgrade, as it were) your
74 L<Moose> class to a singleton.
76 All you should need to do to transform your class is to change C<use Moose> to
77 C<use MooseX::Singleton>. This module uses metaclass roles to do its magic, so
78 it should cooperate with most other C<MooseX> modules.
82 A singleton class will have the following additional methods:
84 =head2 Singleton->instance
86 This returns the singleton instance for the given package. This method does
87 I<not> accept any arguments. If the instance does not yet exist, it is created
88 with its defaults values. This means that if your singleton requires
89 arguments, calling C<instance> will die if the object has not already been
92 =head2 Singleton->initialize(%args)
94 This method can be called I<only once per class>. It explicitly initializes
95 the singleton object with the given arguments.
97 =head2 Singleton->_clear_instance
99 This clears the existing singleton instance for the class. Obviously, this is
100 meant for use only inside the class itself.
102 =head2 Singleton->new
104 This method currently works like a hybrid of C<initialize> and
105 C<instance>. However, calling C<new> directly will probably be deprecated in a
106 future release. Instead, call C<initialize> or C<instance> as appropriate.
110 Please report any bugs or feature requests to
111 C<bug-moosex-singleton@rt.cpan.org>, or through the web interface at
112 L<http://rt.cpan.org>. We will be notified, and then you'll automatically be
113 notified of progress on your bug as we make changes.
117 Shawn M Moore E<lt>sartak@gmail.comE<gt>
119 Dave Rolsky E<lt>autarch@urth.orgE<gt>
121 =head1 SOME CODE STOLEN FROM
123 Anders Nor Berle E<lt>debolaz@gmail.comE<gt>
125 =head1 AND PATCHES FROM
127 Ricardo SIGNES E<lt>rjbs@cpan.orgE<gt>
129 =head1 COPYRIGHT AND LICENSE
131 Copyright 2007-2009 Infinity Interactive
133 This program is free software; you can redistribute it and/or modify it under
134 the same terms as Perl itself.