Hello Monks, yesterday I posted a question about refactoring code for a company and now I have another question about doing so. The previous writers of the code never used use strict/use warnings so I'm trying to make it so that the code uses these. Needless to say the moment I add them a bunch of scope errors get thrown. My question is such, there is a script that contains purely hashes that contain "global" info. Such as work directories, server names, departments, etc. These hash structures are modified by a subroutine that reads a config file and adds entries to the hash to reflect the contents of the config file, and then this configured hash is used in the main automation scripts. It works currently without strict/warnings but when I turn it on it complains. I believe I read something about packages and using "our" for the hashes but I haven't read anything specific enough to my issue to trust it completely. Can anyone help me set up the files correctly for this kind of processing? e.g. 1. Global hash lives in a separate folder, 2. Said hash is modified by a subroutine only once when scripts called(this was done by a config subroutine that lives in a script that was required by another script, I believe that when this script was required it invoked the subroutine and configured the hash), 3. this global hash is properly accessible by all other automation scripts. Thank you for any insight I greatly appreciate it.
EDIT(Sorry for not being detailed enough):
So I have a hash that lives in one script, say global.pl, it looks like this:
%GLOBAL = {
dir1 => "/path/to/directory"
dir2 => "/path/to/another"
}
Then I have another script that contains a subroutine to modify this hash, as well as a call to said subroutine, I believe that this call gets run when the script is "required". This is how that script looks, lets call it config.pl:
[requires at the top]
read_config_files();
sub read_config_files(){
#Do operations that read .txt files and load
#info into the GLOBAL hash structure.
}
Now these previous scripts are never explicitly called, the main automation script looks like this, let's call it main.pl:
#A few requires...
require "global.pl";
require "config.pl";
my first_directory = $GLOBAL{dir1};
etc...
So that after the config.pl is "required" it executes the read_config_files subroutine and the %GLOBAL hash looks like this:
%GLOBAL = {
dir1 = "/path/to/directory",
dir2 = "/path/to/another",
newstuff = "a string that was added",
}
But I get a "Global symbol "GLOBAL" requires explicit package name at main.pl line xx.
My question is how can I properly have this working so that the error does not get thrown. Or, how can I convert the way this is working. Note that it is not just main.pl that tries to access members of %GLOBAL this way, there are a few other scripts that get their global information from this hash, therefore I'm trying to only have on copy of the hash and centralize all the info to that hash.
UPDATE:
I've managed to fix the problem by putting the hashes into a package! Thanks for all your help
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.