perl - 如何在 perl 中实现单例类?

标签 perl singleton

实现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/

相关文章:

perl - 将 Modperl 配置转换为 Plack - 按文件扩展名运行不同的处理程序

java - 在 Java 中实现单例模式的有效方法是什么?

spring-boot - 将 Spring Boot 属性注入(inject) Kotlin Singleton/object

python - 使用单例作为计数器

dependency-injection - DDD : Service and Repositories Instances Injected with DI as Singletons

java - 消除单例

perl - 我怎样才能阻止 Perl 的 Mail::Box::Manager 删除目录?

python - 从缺少条目的多列文件中提取数据

mysql - 如何在 Perl 中从 MySQL 获取最后一个错误

perl - 如何将函数传递给构造函数