NAME
App::Basis::Config - Manage config YAML files in a simple manner
VERSION
version 1.2
SYNOPSIS
use App::Basis::Config
my $cfg = App::Basis::Config->new( filename => "filename") ;
# don't allow the store method to run, we don't want our configdata overwritten
# my $cfg = App::Basis::Config->new( filename => "filename", nostore => 1) ;
my $data = $cfg->raw ;
my $name = $cfg->get( 'name') ;
my $value = $cfg->get('/block/bill/item') ;
# now test setting
$cfg->set( 'test1', 123) ;
$cfg->set( 'test2/test3/test4', 124) ;
# saving, beware if saving a complex config file, comments will be lost
# add a filename to save to a new file
# $cfg->store() ; # save to the filename used in new()
$cfg->store( "filename.new") ;
DESCRIPTION
Carrying on from App:Simple, many apps need a way to get and store simple config data, if you need complex the use a database!
This module is an extension to App::Basis to manage YAML config files in a simple manner.
NAME
App::Basis::Config
Notes
Be careful using the save option, especially if the config is pulled in from
many files, it will only write back to a single file
Public Functions
- raw
-
retrieve a hashref of the config data, once it has been parsed from YAML
- has_data
-
test if there is any data in the config
- changed
-
has the config data changed since the last save, or mark it as changed
if( $data->changed) { say "somethings changed" } # my $data = $cfg->raw ; $data->{deep}->{nested}->{item} = 123 ; # mark the data as changed $data->changed( 1) ; # save in the default config file $data->store() ;
Parameter flag optional, used as a getter if flag is missing, otherwise a setter
- error
-
access last error generated (just a descriptive string)
- new
-
Create a new instance of a config object, read config data from passed filename
Parameters passed in a HASH filename - name of file to load/save config from nostore - prevent store operation (optional) die_on_error - die if we have any errors
- store
-
Saves the config data, will not maintain any comments from the original file. Will not perform save if no changes have been noted.
Parameter filename name of file to store config to, optional, will use object instanced filename by default
- get
-
Retrieve an item of config data. We use a unix style filepath to separate out the individual elements.
We can also use ':' and '.' as path sepators, so valid paths are
/item/name/thing item.name.thing item:name:thing
The leading separator is not needed and is ignored.
If the path points to a complex config structure, ie array or hash, then that is the data that will be returned.
Parameter filepath path to item to retrieve
#get an item from the config data based on a unix style path my $value = $cfg->get( '/deep/nested/item') ; # this is the same as same as accessing the raw data my $data = $cfg->raw ; my $value = $data->{deep}->{nested}->{item} ;
- set
-
Store an item into the config.
# set the value of an item into the config data based on a unix style path # this will mark the data as changed $cfg->set( '/deep/nested/item', 123) ; # same as accessing the raw_data, but this will not mark the data as changed # my $data = $cfg->raw ; $data->{deep}->{nested}->{item} = 123 ; # mark the data as changed, ready for a store operation $data->changed( 1) ;
Parameter filepath path to item to retrieve item item to store, can be scalar, hashref or arrayref
- clear
-
Clear all the data from the config, mark the config data as changed
AUTHOR
Kevin Mulholland <moodfarm@cpan.org>
COPYRIGHT AND LICENSE
This software is copyright (c) 2017 by Kevin Mulholland.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.