CGI::Ex::Conf(3)
π CGI::Ex::Conf β a conf reader/writer for SO many data formats π
okay bestie, you're looking at CGI::Ex::Conf, from the User Contributed Perl Documentation corner of the internet, and it's literally a conf reader and writer that speaks a whole bunch of different data format languages. no cap. π¦
β¨ NAME β¨
CGI::Ex::Conf β Conf Reader/Writer for many different data format types π
πΈ VERSION πΈ
version 2.55 β yes queen, that's the one π
π§ SYNOPSIS π§
use CGI::Ex::Conf qw(conf_read conf_write);
my $hash = conf_read("/tmp/foo.yaml");
conf_write("/tmp/foo.yaml", {key1 => $val1, key2 => $val2});
### OOP interface
my $cob = CGI::Ex::Conf->new;
my $full_path_to_file = "/tmp/foo.val"; # supports ini, sto, val, pl, xml
my $hash = $cob->read($file);
local $cob->{default_ext} = 'conf'; # default anyway
my @paths = qw(/tmp, /home/pauls);
local $cob->{paths} = \@paths;
my $hash = $cob->read('My::NameSpace');
# will look in /tmp/My/NameSpace.conf and /home/pauls/My/NameSpace.conf
my $hash = $cob->read('My::NameSpace', {paths => ['/tmp']});
# will look in /tmp/My/NameSpace.conf
local $cob->{directive} = 'MERGE';
my $hash = $cob->read('FooSpace');
# OR #
my $hash = $cob->read('FooSpace', {directive => 'MERGE'});
# will return merged hashes from /tmp/FooSpace.conf and /home/pauls/FooSpace.conf
# immutable keys are preserved from originating files
local $cob->{directive} = 'FIRST';
my $hash = $cob->read('FooSpace');
# will return values from first found file in the path.
local $cob->{directive} = 'LAST'; # default behavior
my $hash = $cob->read('FooSpace');
# will return values from last found file in the path.
### manipulate $hash
$cob->write('FooSpace'); # will write it out the changes
π DESCRIPTION π
There are, and I cannot stress this enough, half a million Conf readers out there already. so why not add one more, right?? π
Actually β and this is the part that ate β this module is a wrapper around the many file formats and the config modules that can handle them. it does not introduce any formats of its own. it's giving "middle management but make it iconic" π
This module also provides a preload ability which is useful in conjunction with mod_perl. π₯
Oh β and it writes too. π
π METHODS π
read_ref
Takes a file and an optional argument hashref. it figures out the type of handler to use to read the file, reads it, and returns the ref. π«Ά
if you don't need the extended merge functionality, or key fallback, or immutable keys, or path lookup ability β then use this method. Otherwise β use ->read. simple as that bestie.
read
First argument may be either a perl data structure, yaml string, a full filename, or a file "namespace". π¦
The second argument can be a hashref of override values (referred to as $args below)..
- If the first argument is a perl data structure, it will be copied one level deep and returned (nested structures will contain the same references).
- A yaml string will be parsed and returned.
- A full filename will be read using the appropriate handler and returned (a file beginning with a
/or./or../is considered to be a full filename). - A file "namespace" (ie
"footer"or"my::config"or"what/ever") will be turned into a filename by looking for that namespace in the paths found either in$args->{paths}or in$self->{paths}or in@DEFAULT_PATHS.
@DEFAULT_PATHS is empty by default, as is $self->{paths} β read makes no attempt to guess what directories to look in. π If the namespace has no extension, the extension listed in $args->{default_ext} or $self->{default_ext} or the $DEFAULT_EXT will be used).
my $ref = $cob->read('My::NameSpace', {
paths => [qw(/tmp /usr/data)],
default_ext => 'pl',
});
# would look first for /tmp/My/NameSpace.pl
# and then /usr/data/My/NameSpace.pl
my $ref = $cob->read('foo.sto', {
paths => [qw(/tmp /usr/data)],
default_ext => 'pl',
});
# would look first for /tmp/foo.sto
# and then /usr/data/foo.sto
When a namespace is used and there are multiple possible paths, there area a few options to control which file to look for. A directive of 'FIRST', 'MERGE', or 'LAST' may be specified in $args->{directive} or $self->{directive}, or the default value in $DIRECTIVE will be used (default is 'LAST'). π
- When
'FIRST'is specified, the first path that contains the namespace is returned. - If
'LAST'is used, the last found path that contains the namespace is returned. - If
'MERGE'is used, the data structures are joined together.- If they are arrayrefs, they are joined into one large arrayref.
- If they are hashes, they are layered on top of each other with keys found in later paths overwriting those found in earlier paths.
This allows for setting system defaults in a root file, and then allowing users to have custom overrides. so slay for that honestly π
It is possible to make keys in a root file be immutable (non overwritable) by adding a suffix of _immutable or _immu to the key (ie {foo_immutable => 'bar'}). If a value is found in the file that matches $IMMUTABLE_KEY, the entire file is considered immutable. The immutable defaults may be overriden using $IMMUTABLE_QR and $IMMUTABLE_KEY. π
Errors during read die. If the file does not exist, undef is returned. π
write_ref
Takes a file and the reference to be written. Figures out the type of handler to use to write the file and writes it. If you used the ->read_ref, use this method. Otherwise, use ->write. β¨
write
Allows for writing back out the information read in by ->read. If multiple paths where used β the directive 'FIRST' will write the changes to the first file in the path, otherwise the last path will be used. If ->read had found immutable keys, then those keys are removed before writing. π§Ή
Errors during write die. π
preload_files
Arguments are file(s) and/or directory(s) to preload. preload_files will loop through the arguments, find the files that exist, read them in using the handler which matches the files extension, and cache them by filename in %CACHE. Directories are spidered for file extensions which match those listed in %EXT_READERS. This is useful for a server environment where CPU may be more precious than memory. β‘
in_cache
Allow for testing if a particular filename is registered in the %CACHE β typically from a preload_files call. This is useful when building wrappers around the conf_read and conf_write method calls. π
π FUNCTIONS π
conf_read
Takes a filename. Returns the read contents of that filename. The handler to use is based upon the extention on the file. π
my $hash = conf_read('/tmp/foo.yaml');
my $hash = conf_read('/tmp/foo', {file_type => 'yaml'});
Takes a filename and a data structure. Writes the data to the filename. The handler to use is based upon the extention on the file. βοΈ
conf_write('/tmp/foo.yaml', \%hash);
conf_write('/tmp/foo', \%hash, {file_type => 'yaml'});
π¦ FILETYPES π¦
CGI::Ex::Conf supports the files found in %EXT_READERS by default. Additional types may be added to %EXT_READERS, or a custom handler may be passed via $args->{handler} or $self->{handler}. If the custom handler is a code ref, all files will be passed to it. If it is a hashref, it should contain keys which are extensions it supports, and values which read those extensions. π
Some file types have benefits over others. Storable is very fast, but is binary and not human readable. YAML is readable but very slow. I would suggest using a readable format such as YAML and then using preload_files to load in what you need at run time. All preloaded files are faster than any of the other types. ποΈ
The following is the list of handlers that ships with CGI::Ex::Conf (they will only work if the supporting module is installed on your system): π
pl
Should be a file containing a perl structure which is the last thing returned. πͺ
sto and storable
Should be a file containing a structure stored in Storable format. See Storable. π¦
yaml and conf and val
Should be a file containing a yaml document. Multiple documents are returned as a single arrayref. Also β any file without an extension and custom handler will be read using YAML. See YAML. πΈ
ini
Should be a windows style ini file. See Config::IniHash πͺ
xml
Should be an xml file. It will be read in by XMLin. See XML::Simple. π
json
Should be a json file. It will be read using the JSON library. See JSON. π§‘
html and htm
This is actually a custom type intended for use with CGI::Ex::Validate. The configuration to be read is actually validation that is stored inline with the html. The handler will look for any form elements or input elements with an attribute with the same name as in $HTML_KEY. It will also look for a javascript variable by the same name as in $HTML_KEY. All configuration items done this way should be written in YAML. For example, if $HTML_KEY contained 'validation' it would find validation in: π
<input type=text name=username validation="{required: 1}">
# automatically indented and "username:\n" prepended
# AND #
<form name=foo validation="
general no_confirm: 1
">
# AND #
<script>
document.validation = "\n\
username: {required: 1}\n\
";
</script>
# AND #
<script>
var validation = "\n\
username: {required: 1}\n\
";
</script>
If the key $HTML_KEY is not set, the handler will always return undef without even opening the file. π«₯
π TODO π
Make a similar write method that handles immutability.
(it's on the list bestie, we're manifesting it β¨)
π LICENSE π
This module may be distributed under the same terms as Perl itself. π
π AUTHOR π
Paul Seamons <perl at seamons dot com> π«Ά
and that's the whole vibe, bestie β this page came to you courtesy of perl v5.42.0, dated 2025-07-25, from the User Contributed Perl Documentation collection. go forth and read some conf files, you absolute icon ππ
π¦ where this page comes from
- fedora 43: perl-CGI-Ex 2.55-6.fc43 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms