in reply to Re: Modifying/Accessing hash in separate script
in thread Modifying/Accessing hash in separate script

Updated for your pleasure!
  • Comment on Re^2: Modifying/Accessing hash in separate script

Replies are listed 'Best First'.
Re^3: Modifying/Accessing hash in separate script
by Laurent_R (Canon) on Jul 22, 2015 at 20:05 UTC
    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?
Re^3: Modifying/Accessing hash in separate script
by jeffa (Bishop) on Jul 22, 2015 at 19:31 UTC

    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?