I am trying to write a singleton role using Perl and Moose. I understand that the MooseX :: Singleton module is available, but there is always resistance when another CPAN module is required for our project. Having tried this and having worked a little, I would like to understand WHY my method does not work. The singleton role that I wrote is as follows:
package Singleton;
use Moose::Role;
my $_singleInstance;
around 'new' => sub {
my $orig = shift;
my $class = shift;
if (not defined $_singleInstance ){
$_singleInstance = $class->$orig(@_);
}
return $_singleInstance;
};
sub getInstance
{
return __PACKAGE__->new();
}
1;
, singleton. , (ClassA ClassB, ) Singleton, , $_singleInstance. ClassA- > getInstance, ClassA. ClassB- > getInstance script, ClassA ( getInstance ClassB). Singleton ClassA ClassB, . ?