Singleton objects were not singletons when made immutable. We need to
[gitmo/MooseX-Singleton.git] / lib / MooseX / Singleton.pm
1 package MooseX::Singleton;
2 use Moose;
3 use MooseX::Singleton::Object;
4 use MooseX::Singleton::Meta::Class;
5
6 our $VERSION = 0.05;
7
8 sub import {
9     my $caller = caller;
10
11     Moose::init_meta($caller, 'MooseX::Singleton::Object', 'MooseX::Singleton::Meta::Class');
12
13     Moose->import({into => $caller});
14     strict->import;
15     warnings->import;
16 }
17
18 no Moose;
19
20 1;
21
22 __END__
23
24 =pod
25
26 =head1 NAME
27
28 MooseX::Singleton - turn your Moose class into a singleton
29
30 =head1 VERSION
31
32 Version 0.04, released 03 Feb 08
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
53 A singleton is a class that has only one instance in an application.
54 C<MooseX::Singleton> lets you easily upgrade (or downgrade, as it were) your
55 L<Moose> class to a singleton.
56
57 All you should need to do to transform your class is to change C<use Moose> to
58 C<use MooseX::Singleton>. This module uses a new class metaclass and instance
59 metaclass, so if you're doing metamagic you may not be able to use this.
60
61 C<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
63 Alternatively, C<< YourPackage->method >> should just work. This includes
64 accessors.
65
66 =head1 TODO
67
68 =over
69
70 =item Always more tests and doc
71
72 =item Fix speed boost
73
74 C<instance> invokes C<new> every time C<< Package->method >> is called, which
75 incurs a nontrivial runtime cost. I've implemented a short-circuit for this
76 case, which does eliminate nearly all of the runtime cost. However, it's ugly
77 and should be fixed in a more elegant way.
78
79 =back
80
81 =head1 BUGS
82
83 All complex software has bugs lurking in it, and this module is no 
84 exception. If you find a bug please either email me, or add the bug
85 to cpan-RT.
86
87 =head1 AUTHOR
88
89 Shawn M Moore E<lt>sartak@gmail.comE<gt>
90
91 =head1 SOME CODE STOLEN FROM
92
93 Anders Nor Berle E<lt>debolaz@gmail.comE<gt>
94
95 =head1 AND PATCHES FROM
96
97 Ricardo SIGNES E<lt>rjbs@cpan.orgE<gt>
98
99 =head1 COPYRIGHT AND LICENSE
100
101 Copyright 2007 Shawn M Moore.
102
103 This program is free software; you can redistribute it and/or modify it under
104 the same terms as Perl itself.
105
106 =cut
107