Removing what I'm pretty darn sure is a bogus "use metaclass" (since
[gitmo/MooseX-Singleton.git] / lib / MooseX / Singleton / Object.pm
1 #!/usr/bin/env perl
2 package MooseX::Singleton::Object;
3 use Moose;
4
5 extends 'Moose::Object';
6
7 sub instance { shift->new }
8
9 sub initialize {
10   my ($class, @args) = @_;
11
12   my $existing = $class->meta->existing_singleton;
13   confess "Singleton is already initialized" if $existing;
14
15   return $class->SUPER::new(@args);
16 }
17
18 sub new {
19   my ($class, @args) = @_;
20
21   my $existing = $class->meta->existing_singleton;
22   confess "Singleton is already initialized" if $existing and @args;
23
24   # Otherwise BUILD will be called repeatedly on the existing instance.
25   # -- rjbs, 2008-02-03
26   return $existing if $existing and ! @args;
27
28   return $class->SUPER::new(@args);
29 }
30
31 no Moose;
32
33 1;
34
35 __END__
36
37 =pod
38
39 =head1 NAME
40
41 MooseX::Singleton::Object - base class for MooseX::Singleton
42
43 =head1 DESCRIPTION
44
45 This just adds C<instance> as a shortcut for C<new>.
46
47 =cut
48