1 package MooseX::Singleton;
5 use MooseX::Singleton::Object;
6 use MooseX::Singleton::Meta::Class;
9 $VERSION = eval $VERSION;
11 Moose::Exporter->setup_import_methods( also => 'Moose' );
19 my $caller = $p{for_class};
21 Moose::Util::MetaRole::apply_metaclass_roles(
23 metaclass_roles => ['MooseX::Singleton::Role::Meta::Class'],
24 instance_metaclass_roles =>
25 ['MooseX::Singleton::Role::Meta::Instance'],
26 constructor_class_roles =>
27 ['MooseX::Singleton::Role::Meta::Method::Constructor'],
30 Moose::Util::MetaRole::apply_base_class_roles(
33 ['MooseX::Singleton::Role::Object'],
36 return $caller->meta();
48 MooseX::Singleton - turn your Moose class into a singleton
52 Version 0.18, released 24 May 08
57 use MooseX::Singleton;
61 isa => 'HashRef[Str]',
62 default => sub { \%ENV },
67 delete MyApp->env->{PATH};
68 my $instance = MyApp->instance;
69 my $same = MyApp->instance;
73 A singleton is a class that has only one instance in an application.
74 C<MooseX::Singleton> lets you easily upgrade (or downgrade, as it were) your
75 L<Moose> class to a singleton.
77 All you should need to do to transform your class is to change C<use Moose> to
78 C<use MooseX::Singleton>. This module uses a new class metaclass and instance
79 metaclass, so if you're doing metamagic you may not be able to use this.
81 C<MooseX::Singleton> gives your class an C<instance> method that can be used to
82 get a handle on the singleton. It's actually just an alias for C<new>.
84 Alternatively, C<< YourPackage->method >> should just work. This includes
87 If you need to reset your class's singleton object for some reason (e.g.
88 tests), you can call C<< YourPackage->_clear_instance >>.
94 =item Always more tests and doc
98 C<instance> invokes C<new> every time C<< Package->method >> is called, which
99 incurs a nontrivial runtime cost. I've implemented a short-circuit for this
100 case, which does eliminate nearly all of the runtime cost. However, it's ugly
101 and should be fixed in a more elegant way.
107 All complex software has bugs lurking in it, and this module is no
108 exception. If you find a bug please either email me, or add the bug
113 Shawn M Moore E<lt>sartak@gmail.comE<gt>
115 Dave Rolsky E<lt>autarch@urth.orgE<gt>
117 =head1 SOME CODE STOLEN FROM
119 Anders Nor Berle E<lt>debolaz@gmail.comE<gt>
121 =head1 AND PATCHES FROM
123 Ricardo SIGNES E<lt>rjbs@cpan.orgE<gt>
125 =head1 COPYRIGHT AND LICENSE
127 Copyright 2007, 2008 Infinity Interactive
129 This program is free software; you can redistribute it and/or modify it under
130 the same terms as Perl itself.