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

Hello Monks,
I have program 1 which runs, creates a hash based on some calculation and programs ends.
I have program 2 which runs after program 1, but needs to access the hash created by program 1.
What is the best way to achieve this in perl? I do not have database to store values, but I can use out file. The hash that needs to be shared is not straight forward.

e.g. Hash
my %test_hash = ('a1' =>{'abc' => 1.2, 'abd' => 2.3, 'acd' => 4.5} 'b1' =>{'xyz' => 2.2, 'xya' => 4.5, 'mng' => 10.3} )
I am not sure how do I go about writing this type of hash to a out file and read the file to rebuild hash in the second program.
Appreciate your help.
Thanks,
Stan

Replies are listed 'Best First'.
Re: Caching or using values across multiple programs
by lakshmananindia (Chaplain) on May 06, 2009 at 03:48 UTC
    A simple example
    use strict; use warnings; use Storable; my %table=( a => 1, b => 2, c => 3 ); store \%table, 'file';
    use strict; use warnings; use Storable; use Data::Dumper; my $hashref = retrieve('file'); print Dumper $hashref;
    --Lakshmanan G.

    The great pleasure in my life is doing what people say you cannot do.


      Thanks Lakshmanan. Storeable is the way to go
      Appreciate your help.
      Thanks,
      Stan
Re: Caching or using values across multiple programs
by ELISHEVA (Prior) on May 06, 2009 at 05:14 UTC

    Storable does a binary dump. That could be an efficiency bonus or a debugging nightmare. If you want something you can inspect and read, consider either JSON or YAML.

    Personally, I would recommend YAML. I've worked with both and YAML is a lot more flexible than JSON, especially when it comes to hashes. For example,

    • JSON does not handle circular references safely, YAML does.
    • you can control the order of hash keys much more easiliy in YAML than in JSON.
    • JSON dumps blessed objects as undef or throws an exception unless the object's class has a TO_JSON method. YAML defines a default mechanism for dumping objects. If your hash key values are object references, this might be an issue.
    • JSON has no syntax for encoding the class into which an object is blessed. So even if you write a TO_JSON method for the class, you will have to get creative if you want to store the class into which the hash should be blessed.
    • JSON is not friendly to code ref dumps. YAML is. If your hash key values are code references, this might be an issue.

    Despite a subset of nearly alike syntax, there are some small but very differences in YAML and JSON syntax. For example, YAML strings need to end with a newline. JSON barfs when they do. If you only have time to learn one, YAML will cover more use cases and take you farther.

    Best, beth

Re: Caching or using values across multiple programs
by Your Mother (Archbishop) on May 06, 2009 at 04:31 UTC

    Storable is nice (make sure to use nstore) but I'd recommend JSON(::XS) over it. It's human readable/editable, won't break across anything (Storable files can break from version to version and without nstore they are not portable at all). Plus JSON::XS is sometimes even faster than Storable.

    Certain applications lend themselves more to something like memcached. Depending on your needs you might look at that too.

Re: Caching or using values across multiple programs
by nagalenoj (Friar) on May 06, 2009 at 03:54 UTC
    You can use the DBM hash. Using DBM hash the data will get stored in a database file, and at the same time it is as easy as working with hashes. Here, is a sample code to work with DBM hashes,
    #!/usr/bin/perl use strict; use warnings; my %hash; # To open a dbmfile and associate with hash dbmopen(%hash, "nagalenoj", '0666') || die "Can't open database file!" +; $hash{'one'}="1"; $hash{'two'}="2"; $hash{'three'}="3"; #Retrieving values print $hash{'three'}; dbmclose %hash;
Re: Caching or using values across multiple programs
by Anonymous Monk on May 06, 2009 at 03:31 UTC
      Thanks a bunch Monk. This will serve my purpose.
      Thanks,
      Stan
Re: Caching or using values across multiple programs
by ig (Vicar) on May 06, 2009 at 04:40 UTC
Re: Caching or using values across multiple programs
by merlyn (Sage) on May 08, 2009 at 20:37 UTC
    See the DBM::Deep module... transparent shared access to relatively arbitrary Perl data structures.

    -- Randal L. Schwartz, Perl hacker

    The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119.