in reply to Modifying/Accessing hash in separate script

Please show your code or a subset thereof, it is next to impossible to answer your question without having a deeper understanding of what this code does.

In general terms, I do not think that you need to use the our built-in for what you describe.

Not even if you really need a global hash. You can just make a "global" hash by just declaring it with my at the top-level of your program file (outside of any specific lexical scope).

Or do you have several code files, with use or require relations between them?

Replies are listed 'Best First'.
Re^2: Modifying/Accessing hash in separate script
by mdskrzypczyk (Novice) on Jul 22, 2015 at 19:22 UTC
    Updated for your pleasure!
      Alright, you've now changed your original post, so that it is now clear to me that you have a main script and a required module. Maybe that was clear before your changes, but, if so, I clearly did not pick it up.

      Well, in this case, yes, using the our function (instead of my) does make sense, it can make the variable available in both code files. Sorry if my previous answer was wrong, I did not understand the full context.

      Although, to tell the truth, I am almost never using our to share variables between different compile units. I tend to prefer to create setter and accessor functions and to export explicitly these subroutines, rather than the variables.

        I've tried adding the our keyword before the hash but it does not seem to have fixed the errors that get thrown in compilation. I still get the same Global symbol requires explicit package name error. Perhaps there is something else I need to do?

      You should be able to scope that global with our

      our %GLOBAL = { dir1 => "/path/to/directory" dir2 => "/path/to/another" };
      You will most likely have more errors to contend with that are not necessarily related. Just deal with them one at a time. (And check in your code each time you successfully remove a compilation error or warning.)

      UPDATE!
      Why is that hash holding a hash reference? It probably should be either

      our %GLOBAL = ( dir1 => "/path/to/directory" dir2 => "/path/to/another" );
      or
      our $GLOBAL = { dir1 => "/path/to/directory" dir2 => "/path/to/another" };
      I sincerely hope that was a typo on your behalf, this looks to be a daunting piece of code to refactor.

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)
      
        You're right, the hash syntax was a typo and I'm sorry if that threw you off. Yes it has proven to be quite the task especially since I was thrown at it with absolutely no perl knowledge prior looking at the code. I really appreciate your help it means a lot to me and shows how great the perl community can really be.
        Adding "our" before the hash declarations in my global.pl still makes the main.pl throw errors when using strict and warnings, the same error: Global symbol "%GLOBAL" requires explicit package name at line xx. Perhaps this wasn't the solution?