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

I use a module for holding all my config-variables, to share them between the main-script and other modules that offer functions for the main-Script.
When i change the value of a config-variable from the main-script, the functions from other modules will still use the original and not the changed value.
Here is a very simple example of my modules and the main-script:

config.pm:
package config; use strict; our $configvariable = 1; return 1; sub setconfigvariable { $configvariable = shift; } sub getconfigvariable { return $configvariable; }
functions.pm:
package functions; use strict; use config; return 1; sub printconfigvariable { print config::getconfigvariable; }
Script.pl:
use strict; use config; use functions; print config::getconfigvariable; # 1 functions::printconfigvariable; # 1 config::setconfigvariable(2); print config::getconfigvariable; # 2 functions::printconfigvariable; # 1
Why does the function from die functions.pm-module not use the changed value? What can i do to get the behaviour i expect? Thanks in advance, Enfileyb

Replies are listed 'Best First'.
Re: Problem with setting Variables in Perl Modules
by cdarke (Prior) on Nov 20, 2009 at 13:05 UTC
    Probably a bad idea to call your module config because there is a base Perl module called Config. That might cause problems on operating systems which do not have case-sensitive filenames, like Windows.

      Not to mention that by convention all lowercase module names are reserved for Perl's use (e.g. strict, warnings, re, yadda yadda). It's not enforced by any mechanism (save the foot you don't shoot not being your own . . .) but be aware you're flouting convention if you do so.

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.

        By convention, lowercase modules names are reserved for pragmas.

        Core modules use both all lowercase and camelcase names.

Re: Problem with setting Variables in Perl Modules
by jethro (Monsignor) on Nov 20, 2009 at 12:54 UTC
    I tried your code and it worked as expected. Output: 1122
Re: Problem with setting Variables in Perl Modules
by Enfileyb (Initiate) on Nov 20, 2009 at 14:11 UTC
    Of course my modules have different names..
    This was just a simple example; i didnt want to post my full code..

    Now that i test exactly this simplified version it works for me too..

    There must be some timing problem in my real functions..

    Well seems i need to do some more intense debugging..

    If i cant find the problem myself i will ask again..


    Thanks for ur help.