Declaring a hash table in one file and using it in another in Perl

I need to create a hash table in one file and use it in another. The reason for this is because the table is my "database" and will be modified, and I want it to sit with all the other data files, not the script files.

How can I use a table in a script?

+3
source share
4 answers

Define your hash table in a global or batch variable. Then use the command doto load the definition into another script:

 datafiles/database.def
 ---------------------------
 package ProjectData;
 our %DATA = ('abc' => 'def', 'ghi' => 'jkl', ...);


 scripts/myscript.pl
 ------------------------
 use strict;
 do 'datafiles/database.def';
 ... do something with %ProjectData::DATA ...
+4
source

Too many ways to do it!

CSV Text:: CSV Text:: CSV_XS.

do perl script. :.

do "config.pl";

, (XML, JSON, yaml,.ini ..).

+1

, .

0

, , , Storable Data:: Dumper, (5.7.3 5.5 ).

/, ala

package MyImportantHash;

use Exporter;

our @EXPORT = qw( %important_hash );

our %important_hash = (
              some_key  => 'some_value',
         );

1;

use MyImportantHash;
say "$_ => $important_hash{$_}" for sort keys %important_hash;

Of course, all this assumes that you do not want the changes in the hash, but in memory, to be automatically displayed in the file. If you do, look no further than davorg.

0
source

Source: https://habr.com/ru/post/1779425/


All Articles