Commit | Line | Data |
---|---|---|
443f4253 | 1 | package MooseX::Singleton; |
443f4253 | 2 | use Moose::Role; |
3 | ||
14676933 | 4 | our $VERSION = 0.02; |
443f4253 | 5 | |
6 | override 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 | 20 | sub instance { |
14676933 | 21 | shift->new(@_); |
443f4253 | 22 | } |
23 | ||
24 | 1; | |
25 |