Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a program that will output the keys and values of a hash:

#!/usr/bin/perl -w %testhash = ( "label1" => "220", "label2" => "221", "label3" => "222", "label4" => "223", ); open (CCOL, ">ccol") || die "Cannot create ccol\n"; while (($key,$value)=each %testhash) { print CCOL "s/{$key}/$value/g\n"; } close (CCOL);
What I want is to be able to get this same result by reading in an external file with paired keys/ values.

My questions:
what's the best format for the external file?
How do I get it to read in in such a way that my output file does not have a bunch of extra line breaks in it?

I'm really new at this-- thanks in advance for the help. I have been reading perl books all day-- I know there's a simple solution but it is escaping me. Thanks

edited: Thu Jan 8 03:08:48 2004 by jeffa - code tags, formatting

Replies are listed 'Best First'.
Re: using an external file as a hash
by liz (Monsignor) on Jan 07, 2004 at 22:54 UTC
    You probably want to have a look at Storable, which allows you to freeze any Perl data-structure in a file, and thaw that into a Perl data-structure at any time (and any program) in the future.

    Liz

Re: using an external file as a hash
by davido (Cardinal) on Jan 08, 2004 at 00:19 UTC
    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

Re: using an external file as a hash
by borisz (Canon) on Jan 07, 2004 at 23:15 UTC
    There are a lot of modules that try to achive that goal. Storable Data::Dump Data::Dumper Yaml Data::DumpXML I like Storable and Data::Dumper most.