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

Greetings Monks,

Can anybody throw some light on storing data structures in Files. Any advice on this would be appreciable. And can anybody give me if thr is any cpan module available already.

To be more precise i need to store a complete data file in form of data structure to a file. This is to be done in run time. So it should not volatilize memory.

Thanks in Advice
update:-
The data structure is of type hash with pure text. -prasanna.k

Replies are listed 'Best First'.
Re: Storing Data stucture in a File
by jZed (Prior) on Sep 01, 2005 at 17:09 UTC
    What kind of "data file" and "data structure"? Is it a relational database structure, then store it in CSV, or XML, or DBM. Is it a perl hash? Use DBM or Storable. We really can't answer without some clue as to what you are talking about.
Re: Storing Data stucture in a File
by sh1tn (Priest) on Sep 01, 2005 at 17:16 UTC
    Do you mean serialization?
    Storable is what you need in this case.


Re: Storing Data stucture in a File
by cmeyer (Pilgrim) on Sep 01, 2005 at 17:10 UTC

    What are your requirements? There are many ways to store data structures in files, and if I understood what you need, then I could give you a reasonable recommendation.

    You might have a look at MLDBM or YAML or Class::DBI ...

    -Colin.

    WHITEPAGES.COM | INC

Re: Storing Data stucture in a File
by eff_i_g (Curate) on Sep 01, 2005 at 17:13 UTC
Re: Storing Data stucture in a File
by blazar (Canon) on Sep 01, 2005 at 17:14 UTC
    There are tons of modules to "store data structures in files". Which one to choose depends on your actual requirements.I have been happy with Storable (which the phrasing of your own question may have suggested to you!) for my own needs. HTH...
Re: Storing Data stucture in a File
by ChrisR (Hermit) on Sep 01, 2005 at 18:08 UTC
    This is how I do it with a hash but there may well be a better way.

    To store a hash:

    open (FILE,">filename"); print FILE Data::Dumper->Dump([\%hashname],['*hashname']); close(FILE);
    To read it back in:
    open (FILE1,"filename"); undef $/; eval <FILE1>; close(FILE1);
Re: Storing Data stucture in a File
by planetscape (Chancellor) on Sep 02, 2005 at 02:24 UTC
Re: Storing Data stucture in a File
by talexb (Chancellor) on Sep 01, 2005 at 19:24 UTC

    I'm not exactly clear on your question, but I'm guessing that YAML will do what you need. I've used it with much success (thanks Ingy); there's also FreezeThaw -- I haven't used it, but it sounds like a similar solution.

    Alex / talexb / Toronto

    "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

      Please use Storable, which is core, and under maintenance, instead of the old FreezeThaw, which hasn't been updated since 2002.

      -Colin.

      WHITEPAGES.COM | INC

        I just looked at Storable on CPAN -- it isn't marked clearly as core, although the Maintenance is now done by the perl5-porters ... line implies that's the case.

        Is there any plan to mark all core CPAN modules as such? I'm sure Perl newbies would greatly appreciate that.

        Alex / talexb / Toronto

        "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds