Use DB_File or some other DBM module to create a DBM file that stores the hash on disk, then just have each script declare a hash and tie() it to the DBM file. Tying a hash changes its behavior; when you tie a hash to a DBM file, you can make the hash absorb the contents of the hash in the DBM file, make it so any changes to the hash also modify the hash in the DBM file, and other neat stuff depending on the DBM library.

This code here creates the hash and stores it the hash.dbm DBM file:

use DB_File; my %hash; tie (%hash, 'DB_File', 'hash.dbm', O_CREAT|O_RDWR) || die $!; %hash=( '000001' => 'ITEM _First', '000002' => 'Item B', '000003' => 'House', '000004' => 'Service C', '000005' => 'Service X', '000006' => 'Service Z', '00007b' => 'Adjust Area', '020902' => 'Campus', '0sdc45' => 'Unit C', '000011' => 'Unit B', '0wed45' => 'Lower Level', '0ws456' => 'Cars', '02100m' => 'Numbers' );

Afterwards, any script that wants to manipulate the hash in hash.dbm need only just tie() the DBM file for reading and writing, like this:

use DB_File; my %hash tie (%hash, 'DB_File', 'hash.dbm', O_RDWR) || die $!;

then modifying %hash will result in modifying the hash stored in hash.dbm.

If you want a hash in one script to be able to copy the contents of the hash in hash.dbm but not to actually modify it, then tie() the hash for reading only instead of for reading and writing:

my %hash tie (%hash, 'DB_File', 'hash.dbm', O_RDONLY) || die $!;

now modifying %hash won't affect the hash stored in hash.dbm.


In reply to Re: Hash Question by William G. Davis
in thread Hash Question 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.