remove shbang from modules
[gitmo/MooseX-Singleton.git] / lib / MooseX / Singleton / Role / Meta / Class.pm
CommitLineData
8eec3c69 1package MooseX::Singleton::Role::Meta::Class;
2use Moose::Role;
a4e5ec1e 3use MooseX::Singleton::Role::Meta::Instance;
4use MooseX::Singleton::Role::Meta::Method::Constructor;
109b110b 5
1de95613 6sub existing_singleton {
3822ace2 7 my ($class) = @_;
8 my $pkg = $class->name;
9
10 no strict 'refs';
11
12 # create exactly one instance
1de95613 13 if (defined ${"$pkg\::singleton"}) {
14 return ${"$pkg\::singleton"};
3822ace2 15 }
16
1de95613 17 return;
18}
19
03e1b8df 20sub clear_singleton {
21 my ($class) = @_;
22 my $pkg = $class->name;
23 no strict 'refs';
24 undef ${"$pkg\::singleton"};
25}
26
0cd38a85 27override _construct_instance => sub {
1de95613 28 my ($class) = @_;
29
30 # create exactly one instance
31 my $existing = $class->existing_singleton;
32 return $existing if $existing;
33
34 my $pkg = $class->name;
35 no strict 'refs';
36 return ${"$pkg\::singleton"} = super;
3822ace2 37};
38
2b4ce4bd 39no Moose;
40
109b110b 411;
42
b375b147 43__END__
44
45=pod
46
47=head1 NAME
48
8eec3c69 49MooseX::Singleton::Role::Meta::Class - Metaclass role for MooseX::Singleton
b375b147 50
51=head1 DESCRIPTION
52
8eec3c69 53This metaclass role makes sure that there is only ever one instance of an
54object for a singleton class. The first call to C<construct_instance> is run
55normally (and then cached). Subsequent calls will return the cached version.
b375b147 56
57=cut
58