NAME
Config::YAML - Simple configuration automation
Version
Version 1.0
Synopsis
Config::YAML is a wrapper around the YAML module which makes reading and writing configuration files simple. Handling multiple config files (e.g. system and per-user, or a gallery app with per-directory configuration) is a snap.
use Config::YAML;
# create Config::YAML object with any desired initial options
# parameters; load system config; set alternate output file
my $c = Config::YAML->new( config => "/usr/share/foo/globalconf",
output => "~/.foorc",
param1 => value1,
param2 => value2,
...
paramN => valueN,
);
# integrate user's own config
$c->read("~/.foorc");
# integrate command line args using Getopt::Long
$rc = GetOptions ( $c,
'param1|p!',
'param2|P',
'paramN|n',
);
# Write configuration state to disk
$c->write;
# simply get params back for use...
do_something() unless $c->{param1};
# or get them more OO-ly if that makes you feel better
do_something_else() if $c->get('param2');
Methods
new
Creates a new Config::YAML object.
my $c = Config::YAML->new( config => initial_config,
output => <output_config>
);
The initial_config argument is required, and specifies the file to be read in during object creation. The output_config file argument, which specifies the file to write config data to, is optional and defaults to the value of initial_config.
For the sake of convenience, Config::YAML doesn't try to strictly enforce its object-orientation. Values read from YAML files are stored as values (scalar, arrayref, hashref) directly in the object hashref, and are accessed as: $c->{scalar} or $c->{array}[idx] or $c->{hash}{key} (and so on down your data structure).
By the same token, any desired configuration defaults can be passed as arguments to the constructor.
my $c = Config::YAML->new( config => "~/.foorc",
foo => "bar",
baz => "quux"
);
All internal state variables follow the _name convention, so do yourself a favor and don't make config variables with underscores as their first character.
get
Returns the value of a parameter
print $c->get('foo');
Provided for people who are skeeved by treating an object as a plain old hashref part of the time.
set
Sets the value of a parameter
print $c->set('foo',1);
Provided for people who are skeeved by treating an object as a plain old hashref part of the time.
fold
Provides a mechanism for the integration of configuration data from any source...
$c->fold(\%data);
...as long as it ends up in a hash.
read
Imports a YAML-formatted config file.
$c->read('/usr/share/fooapp/fooconf');
read() is called at object creation and imports the file specified by the new(config=>) parameter, so there is no need to call it manually unless you have multiple config files.
write
Dump current configuration state to a YAML-formatted flat file.
$c->write;
TODO
The ability to delineate "system" and "user" level configuration, enabling the output of write() to consist only of data from the user's own init files and command line arguments might be nice.
Author
Shawn Boyette (<mdxi@cpan.org>); original implementation by Kirrily "Skud" Robert (as YAML::ConfigFile).
Bugs
Config::YAML ignores the YAML document separation string (
---) because it has no concept of multiple targets for the data coming from a config file.
Please report any bugs or feature requests to bug-yaml-configfile@rt.cpan.org, or through the web interface at http://rt.cpan.org. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
Copyright & License
Copyright 2004 Shawn Boyette, All Rights Reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.