Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

using external file for hashes

by r0adawg (Beadle)
on Jun 04, 2013 at 17:49 UTC ( [id://1037020]=perlquestion: print w/replies, xml ) Need Help??

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

Hello monks,

I would like to use an external file that contains all of my hashes and then reference that file in my scripts.

I'm not sure where to begin, or what search terms are best to learn this process. any pointers and or links to learn are welcome.

I've included a sample of my current hashes that I'm using.

%Real_time = ( 0 => 'No', 1 => 'Yes', ); %Date_format = ( 0 => 'MM/DD/YY', 1 => 'DD/MM/YY', 2 => 'YY/MM/DD', );

Also I don't think I'm correctly calling/reading my file as perl does not appear to report it available using the following code...

BEGIN { no strict 'refs'; no warnings 'uninitialized'; printf("package %s\n", __PACKAGE__); for (qw( unit_id )) { printf("%s: exists:%s defined:%s prototype:%s\n", $_, exists(&{$_}) || 0, defined(&{$_}) || 0, prototype($_), ); } }
returns unit_id: exists:0 defined:0 prototype:
sorry I forgot that little bit.

thank you

Replies are listed 'Best First'.
Re: using external file for hashes
by topher (Scribe) on Jun 05, 2013 at 03:46 UTC

    Is there a reason it needs to be stored in the external file as perl code for hashes? Typically, you would store the data in a data format (CSV, XML, YAML, etc.), and read the data from the file in to build your Perl data structure (hash).

    Alternately, if the data is more than a few entries, you might be better off leaving the data on disk, and accessing it on demand. My personal preference for that is SQLite (unless I have specific requirements that suggest something else). For your case, you might want to look at Tie::Hash or something alone that line. This type of module will store the contents of your hash in a database file. To your program, the hash looks and acts pretty much like a standard hash, but the data isn't stored in memory, it's stored on disk.

    You could also use a module to implement data structure persistence. Something like Storable (or the numerous similar modules) might be useful for you. It will let you store a data structure (including a hash) on disk, and read it back into your program.

    If you are insistent on storing the data as raw Perl code, you should review the documentation on require and do. These can be used to read in perl code from an external source.

    Christopher Cashell
Re: using external file for hashes
by rpnoble419 (Pilgrim) on Jun 05, 2013 at 02:44 UTC

    I use YAML for this as I can share the information between applications. I have also used JSON for this task as well.

    YAML has two methods for this task from the YAML POD

    DumpFile(filepath, list) Writes the YAML stream to a file instead of just returning a string. LoadFile(filepath) Reads the YAML stream from a file instead of a string.
Re: using external file for hashes
by derby (Abbot) on Jun 04, 2013 at 18:10 UTC

    perldoc -f do

    -derby
Re: using external file for hashes
by frozenwithjoy (Priest) on Jun 04, 2013 at 18:17 UTC

    One suggestion: Since your hash keys are just 0-based indices, why not make them arrays?

    $Real_time{0} == $Real_time[0] == 'No'
      some of the hashes are not zero based. I'm working with a legacy program.

        I see. How about using Data::Dumper to make a file of your hashes? Then read this file in (split each structure based on some delimiter you add during the write process and rebuild hashes w/ eval like described in the answers to Read the Dumper data back to a hash.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1037020]
Approved by shmem
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-03-19 10:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found