Whenever I need my program to read and save settings to a file, I like to use XML::Simple. Basically, you can dump a hashref / array ref or any combination thereof into a file. Then, you can read it back into an array / hash as well. Plus, with all the XML modules / packages out there (and for languages other then Perl) it becomes very easy to interface with your config files.

For instance, by using the XML out function, you could change:

my $data = { things => [ { foo => 'bar' }, { foo => 'baz' }, { foo => 'bam' }, ] };

Into XML like:

<things> <foo>bar</foo> <foo>baz</foo> <foo>bam</foo> </things>

And then after calling the function to read XML you can access it right from the hash ref! And, again, I can't stop stressing to you how portable XML is. If, in the future, you want to do something with that data, expand the project, or create a new program to work with the data, it's very simple to do. Of course, using a Perl module for another type of config file will do this for you, but it won't be portable across platforms. (I wish everything were coded in perl, but let's be realistic. Sometimes you need to work with Java/C/ASP/Whatever)


Want to support the EFF and FSF by buying cool stuff? Click here.

In reply to Re: Working with a legable config file by Vautrin
in thread Working with a legable config file by phoenix9

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.