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 | |
b375b147 |
6 | our $VERSION = 0.03; |
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 | |
18 | 1; |
19 | |
b375b147 |
20 | __END__ |
21 | |
22 | =pod |
23 | |
24 | =head1 NAME |
25 | |
26 | MooseX::Singleton - turn your Moose class into a singleton |
27 | |
28 | =head1 VERSION |
29 | |
30 | Version 0.03, released 16 Dec 07 |
31 | |
32 | =head1 SYNOPSIS |
33 | |
34 | package MyApp; |
35 | use MooseX::Singleton; |
36 | |
37 | has env => ( |
38 | is => 'rw', |
39 | isa => 'HashRef[Str]', |
40 | default => sub { \%ENV }, |
41 | ); |
42 | |
43 | package main; |
44 | |
45 | delete MyApp->env->{PATH}; |
46 | my $instance = MyApp->instance; |
47 | my $same = MyApp->instance; |
48 | |
49 | =head1 DESCRIPTION |
50 | |
51 | A singleton is a class that has only one instance in an application. |
52 | C<MooseX::Singleton> lets you easily upgrade (or downgrade, as it were) your |
53 | L<Moose> class to a singleton. |
54 | |
55 | All you should need to do to transform your class is to change C<use Moose> to |
56 | C<use MooseX::Singleton>. This module uses a new class metaclass and instance |
57 | metaclass, so if you're doing metamagic you may not be able to use this. |
58 | |
59 | 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>. |
60 | |
61 | Alternatively, C<< YourPackage->method >> should just work. This includes |
62 | accessors. |
63 | |
64 | =head1 TODO |
65 | |
66 | =over |
67 | |
68 | =item Always more tests and doc |
69 | |
70 | =item Fix speed boost |
71 | |
72 | C<instance> invokes C<new> every time C<< Package->method >> is called, which |
73 | incurs a nontrivial runtime cost. I've implemented a short-circuit for this |
74 | case, which does eliminate nearly all of the runtime cost. However, it's ugly |
75 | and should be fixed in a more elegant way. |
76 | |
77 | =back |
78 | |
79 | =head1 BUGS |
80 | |
81 | All complex software has bugs lurking in it, and this module is no |
82 | exception. If you find a bug please either email me, or add the bug |
83 | to cpan-RT. |
84 | |
85 | =head1 AUTHOR |
86 | |
87 | Shawn M Moore E<lt>sartak@gmail.comE<gt> |
88 | |
89 | =head1 SOME CODE STOLEN FROM |
90 | |
91 | Anders Nor Berle E<lt>debolaz@gmail.comE<gt> |
92 | |
93 | =head1 COPYRIGHT AND LICENSE |
94 | |
95 | Copyright 2007 Shawn M Moore. |
96 | |
97 | This program is free software; you can redistribute it and/or modify it under |
98 | the same terms as Perl itself. |
99 | |
100 | =cut |
101 | |