There are so many options. To some degree, the answer depends on the complexity of the data, the size of the dataset, and those sorts of factors.

A very simple approach is to write key/value pairs out seperated by a delimiter, such as a colon, comma, or pipe character. Put a line break after each key/val pair. Reading it back in, is as easy as reading one line at a time and splitting it on the delimiter.

Another approach is to use the Storable module, which can store data-structures for you quickly and easily. And the same can be read back in just as easily.

Another approach is a variation on what Data::Dumper can be made to do; write your data-set out as executable Perl, and then upon retrieval, eval it. This opens you up to some potential security risks and other cans of worms, but it's a possibility you see in use once in awhile.

Another obvious approach is to just tie your hash directly to a file. Access and use couldn't be easier. Though you do have to be concerned with the fact that this solution (as well as the other ones mentioned so far in this followup node) doesn't scale well to larger projects.

If you need a more scalable solution, think databases. The DBI.pm module can team up with DBD::SQLite or DBD::CSV to create either a self-contained database (or CSV database) without all the hastles of installing and maintaining something like MySQL.

Lots of options, lots to think about. I think that in some cases coming up with the right data storage strategy is the most important design consideration.

I hope the thoughts and links help...


Dave


In reply to Re: using an external file as a hash by davido
in thread using an external file as a hash by Anonymous Monk

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.