NAME
Config::YAML - Simple configuration automation
VERSION
Version 1.22
SYNOPSIS
Config::YAML is a somewhat object-oriented 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 config parameter is required, and must be the first parameter given. It specifies the file to be read in during object creation. If the second parameter is output, then it is taken to specify the file to which configuration data will later be written out. This positional dependancy makes it possible to have another config and/or output parameter passed to the constructor, which will not receive any special treatment. (It is, of course, also safe to have parameters named config and/or output in configuration files or in calls to set.)
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
For the sake of convenience, Config::YAML doesn't try to strictly enforce its object-orientation. Values read from YAML files are stored as parameters directly in the object hashref, and are accessed as
$c->{scalar}
$c->{array}[idx]
$c->{hash}{key}
and so on down your data structure. If this bothers you, get is provided.
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
$c->set('foo',1);
$c->set('bar',"While I pondered, weak and weary...");
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;
The file to be used is specified in the constructor call. See new, above, for details.
AUTHOR
Shawn Boyette (<mdxi@cpan.org>); original implementation by Kirrily "Skud" Robert (as YAML::ConfigFile).
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.
BUGS
getandsetdon't currently work on nested data structures.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.