vinforget has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I've written a script that has a few subs and would like to put the subs into a module (.pm). When I transfer the subs into the module I get problems accessing global vars declared in the script from the module. Does anyone have suggestions on how to use global vars and perl modules. I realize I can pass the vars to the subs, or just keep the code in the script (via subs), but :
a) I am using quite a few vars.
b) the code is being re-used quite often.
Thanks

Replies are listed 'Best First'.
Re: perl global vars in subroutines
by Abigail-II (Bishop) on Feb 16, 2004 at 16:25 UTC
    Does anyone have suggestions on how to use global vars and perl modules.
    Yes, as a general rule: don't.
    I realize I can pass the vars to the subs, or just keep the code in the script (via subs)
    That's the way to go!
    I am using quite a few vars.
    Depending on how much 'quite' is, and what the subs do, it may be a sign you need a different breakdown in subs. Or that you need to pass around structures (hashes, arrays, objects, references...) instead.
    the code is being re-used quite often.
    All the more reason to not use global variables to pass parameters around.

    Abigail

Re: perl global vars in subroutines
by bear0053 (Hermit) on Feb 16, 2004 at 17:07 UTC
    use our
    our global_1; our global_2; require(perl_mod.pm);
    now in your perl_mod.pm you need to include:
    our global_1; our global_2;
    This will allow you to use global variables across perl files. Make sure you use Strict as well.
      As I said a long time ago, Why is 'our' good?

      I've yet to see a good answer to that question.

      (The original question is a sign of someone rapidly digging himself a deep, nasty hole. But I'll let someone else decide whether to write up a rant on modularity.)

      Well, first of all, if the perlmod.pm file starts with package Me;, it's not going to work. The our $global_1; in the main file will refer to $main::global_1;, while the our $global_2; in the module refer to $Me::global_1;.

      But if you don't use a package statement, and have everything in the main package, and you aren't using parameters to pass arguments to subroutines, there's little reason to use strict and our.

      Abigail

Re: perl global vars in subroutines
by coreolyn (Parson) on Feb 16, 2004 at 18:09 UTC

    This is a kludge answer in comparison to the other more esteemed monks, but I've gotten into the habit of utilizing enviornmental vars (i.e $ENV{'VAR'} )for my globals.

      Interesting idea, to say the least.

      But wouldn't it get terribly mixed up if your script was run more than once at the same time? And don't you have to "clean up" the environment variables when you end your code? What happens if your code --heaven forbid-- crashes halfway through before you can clean the envoironment? Is there a way to reset the environment to its pristine state?

      CountZero

      "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

        I only use in non-parallel processed code. As for the ENV itself, when the shell dies the vars die, only code within the created fork and below contain the values.

Re: perl global vars in subroutines
by vinforget (Beadle) on Feb 17, 2004 at 16:21 UTC
    Thanks for the all the help. Very interesting discussion. It actually helped me solidify my decission to use OOP to solve this problem.
    Even though perl is not particularly suited for OOP I think it may help me establish a better program structure, as compared to just using subs, modules, and vars without OOP. OOP implements all these programming techniques, but allows for a more rigid framework for me to work in i.e. ideas like the scope of a variable seem to be inherent to the object/class, instead of trying to think about it too much without OOP. Thanks again. Vince