Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: checking values of variables

by BillKSmith (Monsignor)
on Aug 12, 2013 at 18:47 UTC ( [id://1049163]=note: print w/replies, xml ) Need Help??


in reply to checking values of variables

Are you validating the named parameters of a function or method? If so, see Params::Validate.
Bill

Replies are listed 'Best First'.
Re^2: checking values of variables
by fionbarr (Friar) on Aug 12, 2013 at 19:21 UTC
    here's the whole thing; should have posted it in the 1st place.
    # open configuration file my $cfg = new Config::Simple($config_file) or die "Can't open configuration file: $config_file\n"; $server = $cfg->param('server'); $database = $cfg->param('database'); $user = $cfg->param('user'); $password = $cfg->param('password'); $logdir = $cfg->param('logdir'); $basedir = $cfg->param('basedir'); $inputdir = $cfg->param('inputdir'); $inputextension = $cfg->param('inputextension'); $cleanupdays = $cfg->param('cleanupdays'); $countyid = $cfg->param('countyid'); $unzipdir = $cfg->param('unzipdir'); $sleeptime = $cfg->param('sleeptime'); $mail_host = $cfg->param('mail_host') || 'mail@xxx.org'; $support_addr = $cfg->param('support_addr') || 'support@xxx.org' +; $support_cc = $cfg->param('support_cc') || q{}; $ar_court_text = $cfg->param('ar_court_text'); $ar_court_ori = $cfg->param('ar_court_ori'); $debug_print = $cfg->param('debug_print'); $delete_txt_file = $cfg->param('delete_txt_file'); $from_email_address = $cfg->param('from_email_address'); $to_email_address = $cfg->param('to_email_address'); $cc_email_address = $cfg->param('cc_email_address'); $send_email = $cfg->param('send_email'); $init_version = $cfg->param('init_version'); &help() if ( $server eq q{} || $database eq q{} || $user eq q{} || $password eq q{} || $logdir eq q{} || $basedir eq q{} || $inputdir eq q{} || $inputextension eq q{} || $cleanupdays eq q{} || $countyid eq q{} || $sleeptime eq q{} || $unzipdir eq q{} || $mail_host eq q{} || $support_addr eq q{} || $ar_court_text eq q{} || $ar_court_ori eq q{} || $debug_print eq q{} || $delete_txt_file eq q{} || $from_email_address eq q{} || $to_email_address eq q{} || $send_email eq q{} || $init_version eq q{} );

      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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1049163]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-04-25 23:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found