Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

reading the hash information from a file

by phemal (Sexton)
on Feb 16, 2007 at 07:07 UTC ( #600372=perlquestion: print w/ replies, xml ) Need Help??
phemal has asked for the wisdom of the Perl Monks concerning the following question:

Hi Guys, i have a hash of hash which is stored in a file (example is given below). Is there anyway where we can access this information and store back in variable or hash of hash format. The file contains huge data and part of the information from file is given below.....
our %hash_of_att_db = ( '0x40e00600' => { 'attack_type' => 'backdoor', 'attack_port' => '', 'attack_sig' => 'lib/attackprolib/backdoor/0x40e00600/08-bac +korifice-2000.dmp', 'attack_sig_v6' => 'lib/attackprolib/backdoor/0x40e00600/ipv +6_08-backorifice-2000.dmp', }, '0x40e00500' => { 'attack_type' => 'backdoor', 'attack_port' => '', 'attack_sig' => 'lib/attackprolib/backdoor/0x40e00500/bionet +.dumpnewdump', 'attack_sig_v6' => 'lib/attackprolib/backdoor/0x40e00500/ipv +6_bionet.dump', }, );

Comment on reading the hash information from a file
Download Code
Re: reading the hash information from a file
by TOD (Friar) on Feb 16, 2007 at 07:14 UTC
    take a look at Storable.pm
Re: reading the hash information from a file
by bobf (Monsignor) on Feb 16, 2007 at 07:14 UTC

    I'm not exactly sure I understand your question, but if you've got a hash dumped to a file (from something like print Dumper( \%hash )) and you trust the file source, you could read it in to a string and eval it.

    If you are using the file as a simplified database, you may want to consider DBM::Deep or Storable.

      ... or YAML

      --
      b10m

      All code is usually tested, but rarely trusted.
Re: reading the hash information from a file
by fenLisesi (Priest) on Feb 16, 2007 at 09:26 UTC
    The following implementation of one of bobf's suggestions may give you some ideas (it does not use strict, so watch out):
    use warnings; use Data::Dumper; my $slurp = do { local $/; <DATA>; }; eval $slurp; no warnings 'once'; print Dumper( \%hash_of_att_db ); __END__ our %hash_of_att_db = ( '0x40e00600' => { 'attack_type' => 'backdoor', 'attack_port' => '', 'attack_sig' => 'lib/attackprolib/fice-2000.dmp', 'attack_sig_v6' => 'lib/attackprolib/fice-2000.dmp', }, '0x40e00500' => { 'attack_type' => 'backdoor', 'attack_port' => '', 'attack_sig' => 'lib/attackprolib/mpnewdump', 'attack_sig_v6' => 'lib/attackprolib/6_bionet.dump', }, );
    It prints: BTW, what do you have at the top of this file? Can you make it a module and use it?

    Cheers

      i dont have anything at the top of the file. let assume that the file has the above data only and lets give anyname.
        Well, I am sure wiser Monks will tell you much better ways of doing this, but if you can name it, say, Backup_Data.pm, and make it look like the following:
        package Backup_Data; our %hash_of_att_db = ( '0x40e00600' => { 'attack_type' => 'backdoor', 'attack_port' => '', 'attack_sig' => 'lib/attackprolib/fice-2000.dmp', 'attack_sig_v6' => 'lib/attackprolib/fice-2000.dmp', }, '0x40e00500' => { 'attack_type' => 'backdoor', 'attack_port' => '', 'attack_sig' => 'lib/attackprolib/mpnewdump', 'attack_sig_v6' => 'lib/attackprolib/6_bionet.dump', }, ); 1;
        (note the first line and the last) then, you could use the file as follows:
        use strict; use warnings; use lib '/home/phemal'; # wherever Backup_Data.pm is use Backup_Data; use Data::Dumper; print Dumper( \%Backup_Data::hash_of_att_db );
        That is to say, our %foo; declared in Backup_Data.pm should now be available to you as %Backup_Data::foo inside your program, which now uses strict. Hope this helps.
Re: reading the hash information from a file
by palette (Scribe) on Feb 16, 2007 at 14:04 UTC
    If I have understood your problem then I think the code below can be one of the solution.

    As In Perl they say there are more than one ways to do it.

    Basically you want some form in which you can store the data to a file then you can collect the data back from the file to a variable. One way can be using Dumper, you can even use Storable.pm

    You can store this hash like this in a file

    use Data::Dumper; open(OUT,">/usr/home/hashtree"); print OUT Dumper(\%tree); close(OUT);


    then whereever u want to retrieve the value back from this file to a variable you can use something like this.

    use Data::Dumper; my $hashtree = do("/usr/home/psivaram/tree") or die $!; %tree = %{$tree};
Re: reading the hash information from a file
by dragonchild (Archbishop) on Feb 16, 2007 at 17:24 UTC
    Given that you have "huge data", you will want to use DBM::Deep. This is one of the reasons this module was written. If you have any questions, please don't hesitate to ask me - I'm the current maintainer.

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2014-10-31 11:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (216 votes), past polls