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::Yacp - Yet Another Configuration file Parser

SYNOPSIS

use Config::Yacp; my $cfg=Config::Yacp->new("config.ini",";");

# Get the names of the sections my @sections=$cfg->get_sections;

# Get the parameter names within a section my @params = $cfg->get_parameters("Section1");

# Get the value of a specific parameter within a section my $value = $cfg->get_value("Section1","Parameter1");

# Add a section $cfg->add_section("Section3");

# Add a parameter and value to a section with a comment $cfg->add_parameter("Section3","Key5","Value5","Comment");

# Change the value of a parameter within a section $cfg->set_value("Section3","Key5","Value99");

# Delete a parameter and value in a section $cfg->del_parameter("Section1","key1");

# Delete an entire section $cfg->del_section("Section2");

# Add a comment to a section/parameter $cfg->add_comment("Section3","key4","This is a comment");

# Change the comment $cfg->set_comment("Section3","key4","New comment");

# Delete a comment $cfg->del_comment("Section3","key4");

# Save the changes to the .ini file $cfg->save_ini;

DESCRIPTION

new

my $comment_marker=";"; my $cfg=Config::Yacp->new("config.ini",$comment_marker);

This constructor returns a reference to a hash. It uses Parse::RecDescent and a simple grammar to parse out the ini file, and put it into a hash. The ini files are similar to those used by Windows, i.e. section names are surrounded by [], and the parameter/values are separated by an = sign. The ini file can contain a single line comment for each key/value. The comment must start with either a semi-colon or a pound sign. The default comment marker is the pound sign.

get_sections

my @sections = $cfg->get_sections;

This method retrieves the section names in a list format

get_parameters

my @params = $cfg->get_parameters("Section1");

This method retrieves the parameter names for a given section and returns them in a list format. This method will croak if a section name is not passed to it or if the section name given to it does not exist.

get_value

my $value = $cfg->get_value("Section1","Parameter1");

This method retrieves the value of the section and parameter that are passed to it. It will croak if either the section name or parameter name does not exist, or if there aren't enough arguments passed to it.

get_comment

my $comment = $cfg->get_comment("Section1","Parameter2");

This method retrieve the comment associated with the Parameter if it exists. If the comment does not exist, it will return with a warning.

add_section

$cfg->add_section("Section3");

This method will add a section into the object, but will not exist in the .ini file until the save_ini method is called. This method will croak if the section being added already exists within the object.

add_parameter

$cfg->add_parameter("Section3","key5","value5","comment");

This method will add a parameter and value to a specific section within the object. The parameter will exist only within the object until the save_ini method is called. This method will croak if it is passed an invalid section name, or the parameter exists within the section. The comment is an optional item that can be passed to the method.

add_comment

$cfg->add_comment("Section1","key2","This is a comment");

This method will add a comment to the section/parameter that is passed to it. If that section/parameter has a comment associated with it, this method will call the set_comment method, otherwise it will push the comment onto the anonymous array.

set_value

$cfg->set_value("Section3","key5","value9");

This method will change the value of the specified section/parameter within the object, and will write the change to the .ini file upon callinig the save_ini method. This method will croak if either the section or parameter name does not exist.

set_comment

$cfg->set_comment("Section3","key5","This is a comment");

This method will change the comment that is contained in the parameter that is passed to it. If the comment doesn't exist for that parameter, it will call the add_comment method, otherwise it will write over the comment.

del_parameter

$cfg->del_parameter("Section3","key5");

This method will delete the specified parameter within a section. The change will occur within the object and is written out to the .ini file upon calling the save_ini method. This method will croak if either the section or parameter name is non-existent.

del_section

$cfg->del_section("Section3");

This method will delete the specified section inside the object. It will also remove any parameters that were under that section heading. This method will croak if the section does not exist.

del_comment

$cfg->del_comment("Section1","key1");

This method will delete the comment associated with the section/parameter passed to it.

save_ini

$cfg->save_ini;

This method will save the parameters that have been changed inside the object back to the ini file that was specified when the object was created. For any comments saved to the ini file, it will use the internal CM parameter, which is the pound sign by default, unless it was set to a semi-colon at the creation of the object.

EXPORT

None by default

ACKNOWLEDGEMENTS

I got the idea for this from the book "Data Munging with Perl", written by Dave Cross.

I would also like to thank everyone at PerlMonks.org who helped me with the questions I had concerning the P::RD module

AUTHOR

Thomas J. Stanley Jr.

Thomas_J_Stanley@msn.com

I can also be found at http://www.perlmonks.org as TStanley. You can also direct any questions concerning this module there

COPYRIGHT

Copyright E 2003 Thomas Stanley. All rights reserved. This program is free software; you can distribute it and/or modify it under the same terms as Perl itself.

SEE ALSO

perl

Parse::RecDescent

Data::Dumper

Fcntl

Test::More