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

I m new to perl and would like to know if I can store multivalued hashes to disk. The Hash::MultiValue module does allow to store multiple values for a key, but does not allow tie to database. Can anyone please provide me with suggestion or pointers/guidance on the same? Thanks in advance! Abhijit
  • Comment on how to store multivalues hashes to database

Replies are listed 'Best First'.
Re: how to store multivalues hashes to database
by shmem (Chancellor) on Feb 03, 2014 at 10:01 UTC

    If that module doesn't provide an interface to a database (what database do you have in mind?), you will have to roll your own, by extending that module via a subclass or providing methods in that module, preferable via a patch.

    If that is not the way for you to go, consider using a different module. The DB_Btree part of DB_File (interface to Berkeley DB) provides multivalue support, and it is pretty fast.

    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
Re: how to store multivalues hashes to database
by karlgoethebier (Abbot) on Feb 03, 2014 at 08:44 UTC

    MLDBM?

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

Re: how to store multivalues hashes to database
by tangent (Parson) on Feb 03, 2014 at 14:21 UTC
    There are a number of approaches you can take to store multi-valued hashes to disk. You mention "tie" so i will assume you want to tie a file, i.e. you want to be able to treat the stored hash as a normal hash. The MLDBM module mentioned by karlgoethebier allows you to store multi-level hashes by serializing the values and is very flexible.

    If your values always follow the same format (i.e. are not arbitrary) then you can use DB_File or the more advanced BerkeleyDB directly and install your own serialization methods via DBM filters. These filters are invoked transparently every time you read or write a value to the database.

    If you can show a sample of your hash structure some other methods can be suggested.

Re: how to store multivalues hashes to database
by jellisii2 (Hermit) on Feb 03, 2014 at 18:56 UTC
    It sounds like you want to serialize data. Look to YAML, JSON, XML, or any of another list of things to get this done.
Re: how to store multivalues hashes to database
by daverowntree (Initiate) on Feb 03, 2014 at 15:40 UTC
    If you merely want to *store* data on disk, rather than use a database, you could simply use Data::Dumper to dump a data structure to a file. Then, you want to build a 'hash of arrays' to store multiple key values, like this:
    use Data::Dumper; my %HoA = (); # crude code to illustrate: $HoA{'key1'} = ['1', '2', '3']; $HoA{'key2'} = ['a', 'b']; # etc. # Dump it open($OUTFILE, '>', 'data.dmp'); $Data::Dumper::Indent = 3; $Data::Dumper::Purity = 1; printf $OUTFILE Dumper(\%HoA); close($OUTFILE);
    Then, later you can read it back in, and eval it to access the data.
Re: how to store multivalues hashes to database
by QM (Parson) on Feb 04, 2014 at 12:46 UTC
    I've used DBM::Deep for a pure Perl solution, not requiring any DBMs installed. Also has a 64bit mode to avoid the limit on 4GB filesize for 32-bit.

    Probably not your first choice unless you need the pure Perl aspect, but it does work as advertised.

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

      "I've used DBM::Deep..."

      ..seems like i'm outdated :-(

      Best regards, Karl

      «The Crux of the Biscuit is the Apostrophe»