NAME
Class::Config - Methods From Config Files
SYNOPSIS
use Class::Config;
my $conf = Class::Config->new;
my $obj = $conf->load(\@files, $inherit_from, $filters);
DESCRIPTION
This module generates unique namespaces for new classes that
contain methods created from the key/value pairs from hashes in
configuration files. This is useful for reusing code, such as
modules and scripts for an entire website, but changing certain
parameters based on an environment variable, for instance.
Method names are generated by capitalizing the first letter and
the first letter after each underscore, then adding 'get' in
front. For example, if one of the files in @files contains the
following:
$info = { field1 => 'value1',
field2 => [ 'element1', 'element2' ],
longer_field_name => 'longer value',
};
the methods getField1(), getField2(), and getLongerFieldName()
will be available via the object returned by Class::Config.
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.
METHODS
new()
Creates a Class::Config object.
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->();
AUTHOR
Don Owens <don@owensnet.com>
COPYRIGHT
Copyright (c) 2003 Don Owens
All rights reserved. This program is free software; you can
redistribute it and/or modify it under the same terms as Perl
itself.
VERSION
0.01