sub conf {
shift; # throw away unused class.
# .. do real work here
}
####
my %re_use = (name => 'Alice');
Ex::Mod->new( override=>'whatever', %re_use );
####
use strict;
use warnings;
use lib '.';
require Ex::Mod;
Ex::mod::conf( user => 'Alice' );
my $a = Ex::mod->new();
# Presumably more constructor use wanting user=>'Alice' goes here..
my $b = Ex::mod->new( user=>'Bob');
printf "User a is: %s\n", $a->whoami();
printf "User b is: %s\n", $b->whoami();
####
package Ex::mod;
use strict;
use warnings;
# package-scoped config.
my %conf = (
user => undef,
);
# config routine. NB: FP!
# ::conf( 'key' => 'value' );
sub conf {
my %c = ( @_ );
for (keys %c) {
$conf{$_} = $c{$_} if exists $conf{$_};
}
}
# new(), the constructor.
sub new {
my $class = shift;
$class = ref($class) || $class; # subclass boilerplate.
my %opts = ( @_ );
my $self = {
user => $opts{user} // $conf{user} // '',
};
return bless $self, $class;
}
# whoami() - method to say who this object's user is:
sub whoami {
my $self = shift;
return $self->{user} // undef;
}