use constant CONFIG_ITEMS => [ qw(
server database user
password logdir basedir
inputdir inputextension cleanupdays
countyid unzipdir sleeptime
unzipdir mail_host support_addr
ar_court_text ar_court_ori debug_print
delete_txt_file from_email_address to_email_address
send_email init_version
] );
my %config;
foreach my $item ( CONFIG_ITEMS ) {
my $value = $cfg->param($item);
help($item) unless defined $value and length $value;
$config{$item} = $value;
}
sub help {
die "Parameter $_[0] wasn't set in config file.";
}
It would still be more fun if we were checking for valid config values, rather than just the existence of a value, but at minimum, this seems to be what you're asking for.
If you are checking for validity also, then make CONFIG_ITEMS a hashref such that:
use constant CONFIG_ITEMS => {
server => sub { my $param = shift; ...some Boolean test },
database => sub { ... },
user => ...
};
...or...
use constant CONFIG_ITEMS => {
server => qr/some pattern/,
database => qr/some pattern/,
...
};
And then...
foreach my $item( keys %{CONFIG_ITEMS} ) {
my $value = $cfg->param($item);
help() unless CONFIG_ITEMS->{$item}->($value);
$config{$item} = $value;
}
...for example.
There's probably a module for this, but it's not that hard to implement from scratch for a trivial use. As things get more complex, start looking for a well tested solution.
|