Class-Config
view release on metacpan or search on metacpan
The files are loaded and the methods are set up in an
inheritance hierarchy in the same order they are passed to the
load() method - the entries in the 2nd file are placed into a
class that inherits from the class generated by the entries in
the first file, the entries in the third file inherit from those
in the 2nd file, and so on.
The $inherit_from parameter passed to the load() method
indicates what class, if any, the class generated by the 1st
file should inhert from.
The $filters parameter is an optional set of filters to be run
on each value before being returned. See the documentation on
the load() method below for details.
The configuration files should not contain a package name. The
contents of each file is eval'd in the scope of a unique
package. The package global $info must be set to the hash
reference that you wish to be used for setting up the methods.
Since the configuration files are eval'd, you may write your own
subroutines in the configuration files to make them available as
methods in the package generated. However, they will be
overridden by and methods generated with the same name from the
entries in the $info hash.
This module has been tested on unix only. It currently depends
on device and inode numbers to generate unique namespaces, so it
may not work on non-unix platforms.
=head1 METHODS
=cut
use strict;
{ package Class::Config;
use vars qw($VERSION);
BEGIN {
$VERSION = '0.01'; # also change below in POD!
}
use Class::Config::File;
=pod
=head2 new()
Creates a Class::Config object.
=cut
sub new {
my ($proto) = @_;
my $self = bless {}, ref($proto) || $proto;
return $self;
}
sub _getSubNameSpace {
my ($self, $file) = @_;
my @stat_info = CORE::stat($file);
my ($dev, $ino) = @stat_info[0,1];
return join('_', map { sprintf("%x", $_) } ($dev, $ino));
}
sub loadFile {
my ($self, $file) = @_;
my $name = $self->_getSubNameSpace($file);
my $name_space = "Class::Config::Confs::$name";
my $obj = Class::Config::File->new($file, { name_space => $name_space, path => $file });
$obj->load;
return $obj;
}
=pod
=head2 load($file, $inherit_from, $filters)
Loads the file given by $file and generates a unique package for
the file, converting entries in the hash reference $info into
methods. If $file is a reference to an array, each file name in
the array will be loaded in sequence, each on inheriting from
the file processed before it. The $inherit_from parameter, if
specified, will be used to set up the inheritance for the first
file specified.
The $filters parameter is an optional array of filters to be run
on each value before being returned. Each element of the array
can be specified in one of three ways. For example,
my $filters = [ [ $obj, $method_name, @args ],
[ \&sub_ref, @args],
[ \&sub_ref ]
];
The first filter will result in the method $method_name being
called on the object $obj and passed the value from the
configuration file, and then the arguments in @args, i.e.,
my $cur_val = $obj->$method_name($val, @args);
The second filter will result in the subroutine sub_ref() being
called with the value as its first argument, and @args as the
rest of the arguments, i.e.,
my $cur_val = $sub_ref->($val, @args);
The third filter will result in the subroutine sub_ref() being
called with just the value as its argument, i.e.,
my $cur_val = $sub_ref->();
=cut
sub load {
my ($self, $file, $inherit_from, $filters) = @_;
my $file_list;
if (ref($file) eq 'ARRAY') {
$file_list = $file;
} else {
$file_list = [ $file ];
}
( run in 1.511 second using v1.01-cache-2.11-cpan-39bf76dae61 )