require the latest Moose
[gitmo/MooseX-Singleton.git] / lib / MooseX / Singleton.pm
CommitLineData
443f4253 1package MooseX::Singleton;
ede8dce0 2
0cbe236b 3use Moose 0.82 ();
ede8dce0 4use Moose::Exporter;
109b110b 5use MooseX::Singleton::Object;
6use MooseX::Singleton::Meta::Class;
443f4253 7
ade9ece0 8our $VERSION = '0.18';
ede8dce0 9$VERSION = eval $VERSION;
443f4253 10
ede8dce0 11Moose::Exporter->setup_import_methods( also => 'Moose' );
443f4253 12
ede8dce0 13sub init_meta {
14 shift;
15 Moose->init_meta(
16 @_,
17 base_class => 'MooseX::Singleton::Object',
18 metaclass => 'MooseX::Singleton::Meta::Class',
19 );
443f4253 20}
21
221;
23
b375b147 24__END__
25
26=pod
27
28=head1 NAME
29
30MooseX::Singleton - turn your Moose class into a singleton
31
32=head1 VERSION
33
ade9ece0 34Version 0.18, released 24 May 08
b375b147 35
36=head1 SYNOPSIS
37
38 package MyApp;
39 use MooseX::Singleton;
40
41 has env => (
42 is => 'rw',
43 isa => 'HashRef[Str]',
44 default => sub { \%ENV },
45 );
46
47 package main;
48
49 delete MyApp->env->{PATH};
50 my $instance = MyApp->instance;
51 my $same = MyApp->instance;
52
53=head1 DESCRIPTION
54
55A singleton is a class that has only one instance in an application.
56C<MooseX::Singleton> lets you easily upgrade (or downgrade, as it were) your
57L<Moose> class to a singleton.
58
59All you should need to do to transform your class is to change C<use Moose> to
60C<use MooseX::Singleton>. This module uses a new class metaclass and instance
61metaclass, so if you're doing metamagic you may not be able to use this.
62
03e1b8df 63C<MooseX::Singleton> gives your class an C<instance> method that can be used to
64get a handle on the singleton. It's actually just an alias for C<new>.
b375b147 65
66Alternatively, C<< YourPackage->method >> should just work. This includes
67accessors.
68
03e1b8df 69If you need to reset your class's singleton object for some reason (e.g.
70tests), you can call C<< YourPackage->_clear_instance >>.
71
b375b147 72=head1 TODO
73
74=over
75
76=item Always more tests and doc
77
78=item Fix speed boost
79
80C<instance> invokes C<new> every time C<< Package->method >> is called, which
81incurs a nontrivial runtime cost. I've implemented a short-circuit for this
82case, which does eliminate nearly all of the runtime cost. However, it's ugly
83and should be fixed in a more elegant way.
84
85=back
86
87=head1 BUGS
88
89All complex software has bugs lurking in it, and this module is no
90exception. If you find a bug please either email me, or add the bug
91to cpan-RT.
92
060d6e6b 93=head1 AUTHORS
b375b147 94
95Shawn M Moore E<lt>sartak@gmail.comE<gt>
96
060d6e6b 97Dave Rolsky E<lt>autarch@urth.orgE<gt>
98
b375b147 99=head1 SOME CODE STOLEN FROM
100
101Anders Nor Berle E<lt>debolaz@gmail.comE<gt>
102
d64d5811 103=head1 AND PATCHES FROM
104
105Ricardo SIGNES E<lt>rjbs@cpan.orgE<gt>
106
b375b147 107=head1 COPYRIGHT AND LICENSE
108
aee76780 109Copyright 2007, 2008 Infinity Interactive
b375b147 110
111This program is free software; you can redistribute it and/or modify it under
112the same terms as Perl itself.
113
114=cut
115