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_metaclass_roles(
24 metaclass_roles => ['MooseX::Singleton::Role::Meta::Class'],
25 instance_metaclass_roles =>
26 ['MooseX::Singleton::Role::Meta::Instance'],
27 constructor_class_roles =>
28 ['MooseX::Singleton::Role::Meta::Method::Constructor'],
31 Moose::Util::MetaRole::apply_base_class_roles(
34 ['MooseX::Singleton::Role::Object'],
37 return $caller->meta();
49 MooseX::Singleton - turn your Moose class into a singleton
54 use MooseX::Singleton;
58 isa => 'HashRef[Str]',
59 default => sub { \%ENV },
64 delete MyApp->env->{PATH};
65 my $instance = MyApp->instance;
66 my $same = MyApp->instance;
70 A singleton is a class that has only one instance in an application.
71 C<MooseX::Singleton> lets you easily upgrade (or downgrade, as it were) your
72 L<Moose> class to a singleton.
74 All you should need to do to transform your class is to change C<use Moose> to
75 C<use MooseX::Singleton>. This module uses metaclass roles to do its magic, so
76 it should cooperate with most other C<MooseX> modules.
80 A singleton class will have the following additional methods:
82 =head2 Singleton->instance
84 This returns the singleton instance for the given package. This method does
85 I<not> accept any arguments. If the instance does not yet exist, it is created
86 with its defaults values. This means that if your singleton requires
87 arguments, calling C<instance> will die if the object has not already been
90 =head2 Singleton->initialize(%args)
92 This method can be called I<only once per class>. It explicitly initializes
93 the singleton object with the given arguments.
95 =head2 Singleton->_clear_instance
97 This clears the existing singleton instance for the class. Obviously, this is
98 meant for use only inside the class itself.
100 =head2 Singleton->new
102 This method currently works like a hybrid of C<initialize> and
103 C<instance>. However, calling C<new> directly will probably be deprecated in a
104 future release. Instead, call C<initialize> or C<instance> as appropriate.
108 Please report any bugs or feature requests to
109 C<bug-moosex-singleton@rt.cpan.org>, or through the web interface at
110 L<http://rt.cpan.org>. We will be notified, and then you'll automatically be
111 notified of progress on your bug as we make changes.
115 Shawn M Moore E<lt>sartak@gmail.comE<gt>
117 Dave Rolsky E<lt>autarch@urth.orgE<gt>
119 =head1 SOME CODE STOLEN FROM
121 Anders Nor Berle E<lt>debolaz@gmail.comE<gt>
123 =head1 AND PATCHES FROM
125 Ricardo SIGNES E<lt>rjbs@cpan.orgE<gt>
127 =head1 COPYRIGHT AND LICENSE
129 Copyright 2007-2009 Infinity Interactive
131 This program is free software; you can redistribute it and/or modify it under
132 the same terms as Perl itself.