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;
It now holds all the information about where to find the configuration files and it only loads the data once and then simply passes the object containing our values out when new is called. This reduces our overhead tremendously since we aren't auto generating methods on every call to My::Config->new(). Which is good in a persistent environment.
Our New Base Class
We have to create a new base class to address our changes to our configuration data. Here is our revised Base module.
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;
Three Ways to Get What You Want
In the initial version both IlyaM and Ctrl-z expressed some concern with the interface to the configuration values. In this version we modify this access to allow for the initial method along with two new ways.
Our three ways to access it are:
- As a My::Config object
- As a part of a config method provided by My::Base
- As a method name of the My::Base object generated by the AUTOLOAD
Here are the three tests:
!/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";