I've got a script that is part of a larger collection of "tools" I use on a cron'ly basis for mirroring and administration of some distributed servers in a cluster, and it basically wraps a system binary which takes roughly 40 possible arguments.

What I've got thus far, is modified via an email sent to a specific (secured) account on an internal control box. In this email body is a template, pseudo-XML, which contains the options I want to pass to the system binary in 'key = value' pairs, and looks like this:

<tmpl> foo = 1 bar = SomeThing blort = 2003-06-29 </tmpl>

I detach this template from the body of the email with Mail::Internet as follows:

my $message = new Mail::Internet ([<>]); my @body = @{$message->body()};

From here, I parse it out with Config::General, and end up with things like this:

my %config = $conf->getall; my $template = $conf->obj('tmpl'); my $foo = $config{'tmpl'}->{'foo'}; my $bar = $config{'tmpl'}->{'bar'}; my $blort = $config{'tmpl'}->{'blort'}; # ... up to 40 possible values

When I have these all in scalars, I go through them one-by-one, and validate each, to make sure I didn't pass any typos or other invalid values. for example, the key 'foo' in the template can only take a digit, so I can restrict that, and check it. The next key, 'bar' may only take a common word, starting with a capital letter. I can also check that. You can see how lengthy this gets after 40 possible values need to be stuffed into scalars and checked for proper syntax and validity.

Is there a better way to parse, accept, and validate each of these values from the template, without duplicating 40 separate checks for each value that can be passed?


In reply to A Refactoring We Will Go by hacker

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.