Commit | Line | Data |
109b110b |
1 | #!/usr/bin/env perl |
2 | package MooseX::Singleton::Meta::Class; |
3 | use Moose; |
4 | use MooseX::Singleton::Meta::Instance; |
5 | |
6 | extends 'Moose::Meta::Class'; |
7 | |
8 | sub initialize { |
9 | my $class = shift; |
10 | my $pkg = shift; |
11 | |
12 | $class->SUPER::initialize( |
13 | $pkg, |
14 | instance_metaclass => 'MooseX::Singleton::Meta::Instance', |
15 | @_, |
16 | ); |
17 | }; |
18 | |
3822ace2 |
19 | override construct_instance => sub { |
20 | my ($class) = @_; |
21 | my $pkg = $class->name; |
22 | |
23 | no strict 'refs'; |
24 | |
25 | # create exactly one instance |
26 | if (!defined ${"$pkg\::singleton"}) { |
27 | ${"$pkg\::singleton"} = super; |
28 | } |
29 | |
30 | return ${"$pkg\::singleton"}; |
31 | }; |
32 | |
109b110b |
33 | 1; |
34 | |
b375b147 |
35 | __END__ |
36 | |
37 | =pod |
38 | |
39 | =head1 NAME |
40 | |
41 | MooseX::Singleton::Meta::Class |
42 | |
43 | =head1 DESCRIPTION |
44 | |
45 | This metaclass is where the forcing of one instance occurs. The first call to |
46 | C<construct_instance> is run normally (and then cached). Subsequent calls will |
47 | return the cached version. |
48 | |
49 | =cut |
50 | |