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' );
17 base_class => 'MooseX::Singleton::Object',
18 metaclass => 'MooseX::Singleton::Meta::Class',
30 MooseX::Singleton - turn your Moose class into a singleton
34 Version 0.18, released 24 May 08
39 use MooseX::Singleton;
43 isa => 'HashRef[Str]',
44 default => sub { \%ENV },
49 delete MyApp->env->{PATH};
50 my $instance = MyApp->instance;
51 my $same = MyApp->instance;
55 A singleton is a class that has only one instance in an application.
56 C<MooseX::Singleton> lets you easily upgrade (or downgrade, as it were) your
57 L<Moose> class to a singleton.
59 All you should need to do to transform your class is to change C<use Moose> to
60 C<use MooseX::Singleton>. This module uses a new class metaclass and instance
61 metaclass, so if you're doing metamagic you may not be able to use this.
63 C<MooseX::Singleton> gives your class an C<instance> method that can be used to
64 get a handle on the singleton. It's actually just an alias for C<new>.
66 Alternatively, C<< YourPackage->method >> should just work. This includes
69 If you need to reset your class's singleton object for some reason (e.g.
70 tests), you can call C<< YourPackage->_clear_instance >>.
76 =item Always more tests and doc
80 C<instance> invokes C<new> every time C<< Package->method >> is called, which
81 incurs a nontrivial runtime cost. I've implemented a short-circuit for this
82 case, which does eliminate nearly all of the runtime cost. However, it's ugly
83 and should be fixed in a more elegant way.
89 All complex software has bugs lurking in it, and this module is no
90 exception. If you find a bug please either email me, or add the bug
95 Shawn M Moore E<lt>sartak@gmail.comE<gt>
97 Dave Rolsky E<lt>autarch@urth.orgE<gt>
99 =head1 SOME CODE STOLEN FROM
101 Anders Nor Berle E<lt>debolaz@gmail.comE<gt>
103 =head1 AND PATCHES FROM
105 Ricardo SIGNES E<lt>rjbs@cpan.orgE<gt>
107 =head1 COPYRIGHT AND LICENSE
109 Copyright 2007, 2008 Infinity Interactive
111 This program is free software; you can redistribute it and/or modify it under
112 the same terms as Perl itself.