- singleton
Singleton
examples/perloop/config/lib/Conf.pm
package Conf; use strict; use warnings; my $instance; sub new { my ($class) = @_; die "Called ->new again" if $instance; $instance = {}; bless $instance, $class; # Read the configuration .... return $instance; } sub instance { my ($self) = @_; die "Called ->instance before calling ->new" if not $instance; return $instance; } 1;
examples/perloop/config/script/code.pl
use strict; use warnings; use v5.10; use Conf; my $c = Conf->new; say $c; # Conf=HASH(0x4e7fdc) my $d = Conf->instance; say $c; # Conf=HASH(0x4e7fdc) my $e = Conf->new; # Called ->new again at lib/Conf.pm line 10.
examples/perloop/config/script/too_early.pl
use strict; use warnings; use v5.10; use Conf; my $c = Conf->instance; # Called ->instance before calling ->new at ...