The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

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 used 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 receive no special treatment.

Initial configuration values can be passed as parameters to the constructor:

    my $c = Config::YAML->new( config => "~/.foorc",
                               foo    => "abc",
                               bar    => "xyz",
                               baz    => [ 1, 2, 3 ],
                             );

All internal state variables follow the _name convention, so do yourself a favor and don't pass parameters 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. However, the method get is provided for people who are skeeved by treating an object as a plain old hashref part of the time.

get returns the value of a parameter

    print $c->get('foo');

If the config parameter requested is something other than a scalar, a reference to it will be returned.

set

Also provided is a set method, which sets the value of a parameter:

    $c->set('foo',1);
    $c->set('bar',"While I pondered, weak and weary...");

    my @paints = qw( oil acrylic tempera );
    $c->set('paints', \@paints);

fold

Provides a mechanism for the integration of configuration data from any source...

    $c->fold(\%data);

...as long as it's been munged into 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 written 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

  • Builtin parameter type/value checking would be cool.

  • 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

  • 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.