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;
In reply to Re: Modifying/Accessing hash in separate script
by Anonymous Monk
in thread Modifying/Accessing hash in separate script
by mdskrzypczyk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |