in reply to Modifying/Accessing hash in separate script
and then in read_config_files:%main::GLOBAL = ( dir1 = "/path/to/directory", dir2 = "/path/to/another", );
That's assuming that %GLOBAL lives in the main package, which I suppose it does. For refactoring, perhaps it would be clearer to give it its own package:sub read_config_files { $main::GLOBAL{newstuff} = "a string that was added"; # etc }
and# this is file global.pl use strict; use warnings; package GlobalStuff; %GlobalStuff::GLOBAL = ( dir1 = "/path/to/directory", dir2 = "/path/to/another", ); # or, alternatively (same thing) our %GLOBAL = ( dir1 = "/path/to/directory", dir2 = "/path/to/another", );
Then I would rename 'global.pl' to 'GlobalStuff.pm', and change all calls to require accordingly:# this is file config.pl use strict; use warnings; require 'global.pl'; read_GLOBAL(); print_GLOBAL(); # for example sub read_GLOBAL { $GlobalStuff::GLOBAL{new} = 'a string'; } sub print_GLOBAL { while ( my ($k, $val) = each %GlobalStuff::GLOBAL ) { print "$k => $val\n"; } }
require appends .pm to a bareword and searches this file in @INC; see require for details.require GlobalStuff;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Modifying/Accessing hash in separate script
by mdskrzypczyk (Novice) on Jul 23, 2015 at 13:11 UTC | |
by Laurent_R (Canon) on Jul 23, 2015 at 13:48 UTC |