1MooseX::Singleton(3) User Contributed Perl Documentation MooseX::Singleton(3)
2
3
4
6 MooseX::Singleton - turn your Moose class into a singleton
7
9 package MyApp;
10 use MooseX::Singleton;
11
12 has env => (
13 is => 'rw',
14 isa => 'HashRef[Str]',
15 default => sub { \%ENV },
16 );
17
18 package main;
19
20 delete MyApp->env->{PATH};
21 my $instance = MyApp->instance;
22 my $same = MyApp->instance;
23
25 A singleton is a class that has only one instance in an application.
26 "MooseX::Singleton" lets you easily upgrade (or downgrade, as it were)
27 your Moose class to a singleton.
28
29 All you should need to do to transform your class is to change "use
30 Moose" to "use MooseX::Singleton". This module uses a new class
31 metaclass and instance metaclass, so if you're doing metamagic you may
32 not be able to use this.
33
34 "MooseX::Singleton" gives your class an "instance" method that can be
35 used to get a handle on the singleton. It's actually just an alias for
36 "new".
37
38 Alternatively, "YourPackage->method" should just work. This includes
39 accessors.
40
41 If you need to reset your class's singleton object for some reason
42 (e.g. tests), you can call "YourPackage->_clear_instance".
43
45 Always more tests and doc
46 Fix speed boost
47 "instance" invokes "new" every time "Package->method" is called,
48 which incurs a nontrivial runtime cost. I've implemented a short-
49 circuit for this case, which does eliminate nearly all of the
50 runtime cost. However, it's ugly and should be fixed in a more
51 elegant way.
52
54 All complex software has bugs lurking in it, and this module is no
55 exception. If you find a bug please either email me, or add the bug to
56 cpan-RT.
57
59 Shawn M Moore <sartak@gmail.com>
60
61 Dave Rolsky <autarch@urth.org>
62
64 Anders Nor Berle <debolaz@gmail.com>
65
67 Ricardo SIGNES <rjbs@cpan.org>
68
70 Copyright 2007, 2008 Infinity Interactive
71
72 This program is free software; you can redistribute it and/or modify it
73 under the same terms as Perl itself.
74
75
76
77perl v5.12.0 2009-09-12 MooseX::Singleton(3)