Commit | Line | Data |
443f4253 |
1 | package MooseX::Singleton; |
109b110b |
2 | use Moose; |
3 | use MooseX::Singleton::Object; |
4 | use MooseX::Singleton::Meta::Class; |
443f4253 |
5 | |
86c3868d |
6 | our $VERSION = 0.07; |
443f4253 |
7 | |
109b110b |
8 | sub 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 |
18 | no Moose; |
19 | |
443f4253 |
20 | 1; |
21 | |
b375b147 |
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 | |
86c3868d |
32 | Version 0.07, released 07 Mar 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 | |
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 | |
d64d5811 |
95 | =head1 AND PATCHES FROM |
96 | |
97 | Ricardo SIGNES E<lt>rjbs@cpan.orgE<gt> |
98 | |
b1411a85 |
99 | Dave Rolsky E<lt>autarch@urth.orgE<gt> |
100 | |
b375b147 |
101 | =head1 COPYRIGHT AND LICENSE |
102 | |
b1411a85 |
103 | Copyright 2007, 2008 Shawn M Moore. |
b375b147 |
104 | |
105 | This program is free software; you can redistribute it and/or modify it under |
106 | the same terms as Perl itself. |
107 | |
108 | =cut |
109 | |