NAME
Config::Tree::Dir - Read configuration tree from a directory
SYNOPSIS
# in confdir/
# foo/
# bar # content: '3'
# baz # content: "hello, world!\n\n"
# binary # content: "\xff\xfe\n"
# quux # content: '{i: 1, j: 2}'
# flag # 0 bytes
# in script.pl:
use Config::Tree::Dir;
my $conf = Config::Tree::Dir->new(
path => '/path/to/confdir',
#watch => 10, # currently not implemented
#allow_symlink => 0,
#check_owner => 1,
#content_as_yaml => 0,
#include_path_re => qr/.../,
#exclude_path_re => qr/.../,
#include_file_re => qr/.../,
#exclude_file_re => qr/.../,
#tie_cache_opts => {...} # currently not implemented
ro => 0,
);
# when content_as_yaml is 0:
$conf->get('/foo/bar'); # 3
$conf->get('/foo/baz'); # "hello, world!", newlines stripped
$conf->get('/foo/binary'); # "\xff\xfe\n", newlines not stripped in binaries
$conf->get('/foo/flag'); # 1, all zero byte files is assumed to mean True
# when content_as_yaml is 1:
$conf->get('/foo/bar'); # 3
$conf->get('/foo/baz'); # "hello, world!", YAML parser also strips newlines
$conf->get('/foo/flag'); # undef
$conf->cd('/foo');
$conf->set('bar', 10); # same as set('/foo/bar', 10);
$conf->save(); # writes back to directory
DESCRIPTION
ATTRIBUTES
METHODS
new(%args)
Construct a new Config::Tree::Dir object. Arguments.
path
. Required. Path to config directory.ro
. Optional, default is 0. Whether we should disallow set() and save().allow_symlink
. Optional, default is 0. When is set to 0, then all symlinks will be ignored. This is related to security when superuser/root is dealing with user-created config dirs.check_owner
. Optional, default is 1. When set to 1, then all files/dirs in the config directory must either be created by superuser/root or by config dir owner, otherwise it will be ignored. This is related to security when superuser/root is dealing with user-created config dirs.exclude_path_re
. Optional. When set, config path matching the regex will not be retrieved. See also:include_path_re
.include_path_re
. Optional. When set, only config path matching the regex will be retrieved. Takes precedence overexclude_path_re
.exclude_file_re
. Optional. Default is qr/\A#|~\z/ (backup files). When set, files with name matching the regex will not be read. See also:include_file_re
.include_file_re
. Optional. When set, only files with name matching the regex will be read. Takes precedence overexclude_file_re
.content_as_yaml
. Optional, default is 0. When set to 1, all files are assumed to be YAML documents and will be parsed. Otherwise, these conventions are used when retrieving file contents:- all trailing newlines ("\x0d", "\x0a") will be stripped, unless the file is a binary file (a binary file is defined as a file containing other than ASCII [\x09\x0a\x0d\x20-\x277].
- zero-length files will be retrieved as 1. This is useful for flag files (which indicated active/true when exist and nonactive/false when do not).
file_is_excluded
get_tree_for
set($path, $val)
Set config variable.
Will immediately create necessary subdirectories and write to file.
Example: set("/a/b/c", 1) will create a/ and a/b/ subdirectories, and file a/b/c containing "1". If a already exists as a file, it will be removed
When $val is a reference and content_as_yaml is 1, a YAML dump will be written to the file.
unset($path)
Unset config variable.
save()
Does nothing. All changes are immediately written by set() or unset().
SEE ALSO
Data::Schema, Config::Tree::Base
AUTHOR
Steven Haryanto, <stevenharyanto at gmail.com>
COPYRIGHT & LICENSE
Copyright 2009 Steven Haryanto, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.