Do a bit of fixing. instance and new are one and the same. More tests to ensure that...
[gitmo/MooseX-Singleton.git] / lib / MooseX / Singleton.pm
CommitLineData
443f4253 1package MooseX::Singleton;
443f4253 2use Moose::Role;
3
14676933 4our $VERSION = 0.02;
443f4253 5
6override new => sub {
14676933 7 my ($class) = @_;
443f4253 8
14676933 9 no strict 'refs';
443f4253 10
14676933 11 # create our instance if we don't already have one
12 if (!defined ${"$class\::singleton"}) {
13 ${"$class\::singleton"} = super;
14 }
443f4253 15
14676933 16 return ${"$class\::singleton"};
443f4253 17};
18
14676933 19# instance really is the same as new. any ideas for a better implementation?
443f4253 20sub instance {
14676933 21 shift->new(@_);
443f4253 22}
23
241;
25