Commit | Line | Data |
443f4253 |
1 | package MooseX::Singleton; |
ede8dce0 |
2 | |
3 | use Moose (); |
4 | use Moose::Exporter; |
109b110b |
5 | use MooseX::Singleton::Object; |
6 | use MooseX::Singleton::Meta::Class; |
443f4253 |
7 | |
ede8dce0 |
8 | our $VERSION = '0.09_01'; |
9 | $VERSION = eval $VERSION; |
443f4253 |
10 | |
ede8dce0 |
11 | Moose::Exporter->setup_import_methods( also => 'Moose' ); |
443f4253 |
12 | |
ede8dce0 |
13 | sub 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 | |
22 | 1; |
23 | |
b375b147 |
24 | __END__ |
25 | |
26 | =pod |
27 | |
28 | =head1 NAME |
29 | |
30 | MooseX::Singleton - turn your Moose class into a singleton |
31 | |
32 | =head1 VERSION |
33 | |
911a9dda |
34 | Version 0.08, 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 | |
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. |
58 | |
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. |
62 | |
63 | 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>. |
64 | |
65 | Alternatively, C<< YourPackage->method >> should just work. This includes |
66 | accessors. |
67 | |
68 | =head1 TODO |
69 | |
70 | =over |
71 | |
72 | =item Always more tests and doc |
73 | |
74 | =item Fix speed boost |
75 | |
76 | C<instance> invokes C<new> every time C<< Package->method >> is called, which |
77 | incurs a nontrivial runtime cost. I've implemented a short-circuit for this |
78 | case, which does eliminate nearly all of the runtime cost. However, it's ugly |
79 | and should be fixed in a more elegant way. |
80 | |
81 | =back |
82 | |
83 | =head1 BUGS |
84 | |
85 | All complex software has bugs lurking in it, and this module is no |
86 | exception. If you find a bug please either email me, or add the bug |
87 | to cpan-RT. |
88 | |
89 | =head1 AUTHOR |
90 | |
91 | Shawn M Moore E<lt>sartak@gmail.comE<gt> |
92 | |
93 | =head1 SOME CODE STOLEN FROM |
94 | |
95 | Anders Nor Berle E<lt>debolaz@gmail.comE<gt> |
96 | |
d64d5811 |
97 | =head1 AND PATCHES FROM |
98 | |
99 | Ricardo SIGNES E<lt>rjbs@cpan.orgE<gt> |
100 | |
b1411a85 |
101 | Dave Rolsky E<lt>autarch@urth.orgE<gt> |
102 | |
b375b147 |
103 | =head1 COPYRIGHT AND LICENSE |
104 | |
b1411a85 |
105 | Copyright 2007, 2008 Shawn M Moore. |
b375b147 |
106 | |
107 | This program is free software; you can redistribute it and/or modify it under |
108 | the same terms as Perl itself. |
109 | |
110 | =cut |
111 | |