Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: reading the hash information from a file

by fenLisesi (Priest)
on Feb 16, 2007 at 09:26 UTC ( [id://600389]=note: print w/replies, xml ) Need Help??


in reply to reading the hash information from a file

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:
$VAR1 = { '0x40e00500' => { 'attack_sig_v6' => 'lib/attackprolib/6_bio +net.dump', 'attack_port' => '', 'attack_type' => 'backdoor', 'attack_sig' => 'lib/attackprolib/mpnewdum +p' }, '0x40e00600' => { 'attack_sig_v6' => 'lib/attackprolib/fice- +2000.dmp', 'attack_port' => '', 'attack_type' => 'backdoor', 'attack_sig' => 'lib/attackprolib/fice-200 +0.dmp' } };
BTW, what do you have at the top of this file? Can you make it a module and use it?

Cheers

Replies are listed 'Best First'.
Re^2: reading the hash information from a file
by phemal (Sexton) on Feb 16, 2007 at 09:39 UTC
    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.
        Of course, if you're going to go that route, you could leave off the first and last lines (i.e., don't bother making it a package) and require the file. The required file does still have to be valid perl (such as an assignment statement) and I find it preferable to declare the variables which it sets in the main program rather than in the required file, but this basic technique generally works pretty well for reading in configuration data and the like, provided you can trust the file you're reading in.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-04-25 22:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found