in reply to Modifying/Accessing hash in separate script

You can actually give explicit package name to satisfy strict (like it says). Like this:
%main::GLOBAL = ( dir1 = "/path/to/directory", dir2 = "/path/to/another", );
and then in read_config_files:
sub read_config_files { $main::GLOBAL{newstuff} = "a string that was added"; # etc }
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:
# 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", );
and
# 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"; } }
Then I would rename 'global.pl' to 'GlobalStuff.pm', and change all calls to require accordingly:
require GlobalStuff;
require appends .pm to a bareword and searches this file in @INC; see require for details.

Replies are listed 'Best First'.
Re^2: Modifying/Accessing hash in separate script
by mdskrzypczyk (Novice) on Jul 23, 2015 at 13:11 UTC
    If I do this approach will it allow the read_config_files subroutine to modify the hash and the changes will be reflected across all other scripts that access the hash?
      Yes, it should work (but why don't you test it?).

      Having said that, having a global hash shared among different program files is usually rather poor design. I can't say for sure, because we don't have enough details about what you are doing, but maybe you should share functions accessing to this hash (useing a module), or perhaps think about an OO-implementation.