Amblikai has asked for the wisdom of the Perl Monks concerning the following question:
Hi Monks! I have a quick question regarding configuration files
I almost always use Apache style configuration files in partnership with Config::General in my scripts
The thing is, i always end up writing lengthy subroutines or methods to check that the config file is in the correct format or has the data i'm expecting before i use it. I've been known to make this the majority of the length of my script!
Is this madness? What is the general opinion on checking config files are as expected?
Error out upon trying to use an aspect of the configuration if its wrong?
Completely check that everything is in place with the config so you have complete confidence in moving on and using the config data?
Are there any modules out there to help with this or do i just accept that the end user might completely screw up the config data/format and just bail with an error when i don't get what i expect?
Cheers!
Edit for Completeness!
Thanks for all the replies, i looked into Config::Validator but i couldn't work out how to do it should the config have unknown values in it
So long story short, i ended up creating a child class of Config::General and having a "CFG_check_cfg" method which runs all the other little standalone checks i wrote.
The Package being:
package ConfigObjects; use strict; use warnings; use Exporter 'import'; use parent 'Config::General'; our $VERSION='0.1'; our @EXPORT_OK=qw(CFG_check_cfg); # Main config checker sub CFG_check_cfg { my $self=shift; my $return=1; # True by default (all good) $return&=CFG_check1($self); $return&=CFG_check2($self); return $return; } # Unit checks: sub CFG_check1 { my $self=shift; return (#Some property of the $self->{config} data structure); } 1; __END__
Then i call the config parser in the main script like:
my $cfg_obj=ConfigObjects->new(-ConfigFile => $opt_config_file); pod2usage(-verbose => 1, -exitval => 1, -msg => "ERROR: Config file ha +s errors") unless ($cfg_obj->CFG_check_cfg);
Any feedback would be very much appreciated. Particularly if doing it this way is madness!!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Data checking configuration files
by SimonPratt (Friar) on Sep 17, 2015 at 13:45 UTC | |
by BillKSmith (Monsignor) on Sep 17, 2015 at 20:03 UTC | |
by SimonPratt (Friar) on Sep 18, 2015 at 11:42 UTC | |
|
Re: Data checking configuration files
by nevdka (Pilgrim) on Sep 18, 2015 at 00:23 UTC | |
by Anonymous Monk on Sep 18, 2015 at 00:57 UTC | |
|
Re: Data checking configuration files
by KurtSchwind (Chaplain) on Sep 18, 2015 at 14:00 UTC | |
|
Re: Data checking configuration files
by locked_user sundialsvc4 (Abbot) on Sep 17, 2015 at 11:49 UTC | |
by Amblikai (Scribe) on Sep 17, 2015 at 11:53 UTC | |
| |
|
Re: Data checking configuration files
by Amblikai (Scribe) on Sep 29, 2015 at 11:16 UTC |