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