package My::Config;
use strict;
use base ( 'Class::Accessor' );
use Config::Auto;
my $base = __PACKAGE__;
our $config_hash = {};
bless $config_hash , $base;
my $config = Config::Auto::parse( '/home/trs80/my.conf',
format => 'equal' );
$base->mk_accessors(keys %{$config});
foreach (keys %{$config}) {
$config_hash->$_($config->{$_});
}
sub new {
return $config_hash;
}
1;
####
package My::Base;
use My::Config;
sub new {
my $self = { };
bless $self , 'My::Base';
}
sub config {
my ($self) = @_;
my $config = My::Config->new();
return $config;
}
sub AUTOLOAD {
my ( $self, $value ) = @_;
$AUTOLOAD =~ /.*::(\w+)/;
my $command = $1;
if ( defined($value) ) {
$self->config->$command($value);
return;
} else {
return $self->config->$command;
}
}
1;
##
##
!/usr/bin/perl
# access config data directly from the My::Config class
use My::Config;
use strict;
my $config = My::Config->new();
print $config->base_directory , "\n";
$config->base_directory("/home/otheruser/");
print $config->base_directory() , "\n";
##
##
#!/usr/bin/perl
# access configuration data through the My::Base class
use My::Base;
use strict;
my $object = My::Base->new();
print $object->config->base_directory , "\n";
$object->config->base_directory("/home/otheruser/");
print $object->config->base_directory() , "\n";
##
##
#!/usr/bin/perl
use My::Base;
use strict;
# access configuration data by name through AUTOLOAD
my $object = My::Base->new();
print $object->base_directory , "\n";
$object->base_directory("/home/otheruser/");
print $object->base_directory() , "\n";