First, I think I would store the config file data in a hash, rather than an array. That way you can look up a particular datum more easily, by its name.

As for the code that reads the config file, there are modules on the CPAN for reading config files, see for instance here, although some of those may require the config file to be in a certain format that may not match yours. (On the other hand, if you don't have to accommodate pre-existing config files, you could just define your config file format to be the format that the module reads.)

Now, the other thing you asked about is checking the config data for each of several values and, if they are not present, calling a routine to construct them. If I were doing that, the first thing I'd do is make a list of the values I want (e.g., From, To, Cc, and so forth). The second thing I'd do is make the list a hash, with the name of the header being the key, and the subroutine that constructs the header would be the value. Something along these lines...

my %dispatch = ( From => sub { return '"Nobody" <nobody@localhost>'; }, Subject => sub { die "The GNKSA requires that the subject field alwa +ys be specified by the user."; }, # and so forth. );

The third thing would be to actually do the assignments, and I'd probably use a foreach loop over the keys for that part. Something like this...

for my $k (keys %dispatch) { if (exists $config{$k}) { $value{$k} = $config{$k}; } else { $value{$k} = $dispatch{$k}->(); } }

It is possible to golf that loop down to one line, but you either lose the exists test (resulting in a situation where a user cannot specify a false value) or else have to use the ? : operator, which I find beginners often don't like. Anyway, the dispatch table is going to be most of the work.


Sanity? Oh, yeah, I've got all kinds of sanity. In fact, I've developed whole new kinds of sanity. Why, I've got so much sanity it's driving me crazy.

In reply to Re: Config file default options? by jonadab
in thread Config file default options? by sanjay nayak

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.