实现Singletons 的最佳实践是什么?在 Perl 中?
最佳答案
您可以使用 Class::Singleton模块。
“单例”类也可以使用 my
轻松实现。或 state
变量(后者自 Perl 5.10 起可用)。但请参阅下面的@Michael 评论。
package MySingletonClass;
use strict;
use warnings;
use feature 'state';
sub new {
my ($class) = @_;
state $instance;
if (! defined $instance) {
$instance = bless {}, $class;
}
return $instance;
}
关于perl - 如何在 perl 中实现单例类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2310341/