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.


Dave


In reply to Re^3: checking values of variables by davido
in thread checking values of variables by fionbarr

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.