in reply to Re: Using variables in top level script inside the perl modules
in thread Using variables in top level script inside the perl modules

I don't want to pass the variable as an argument because this foo.pl is an existing object oriented perl script and uses a lot of modules. The "$variable" will be used in a sub-routine in one of the module which is called from few other modules. If I have to pass the variable as an argument I would need to make changes in a lot of perl modules and the top level script which I would like to avoid as far as possible. I tried $::variable in a small dummy perl program but it does not seem to be working for me. Regards.
  • Comment on Re^2: Using variables in top level script inside the perl modules

Replies are listed 'Best First'.
Re^3: Using variables in top level script inside the perl modules
by almut (Canon) on Nov 06, 2008 at 11:01 UTC

    Just a wild guess, but one conceivable reason that $::version would not work is that it's being used before it has been assigned its value. After all - as you have use Arithmetic; - the code is in an implicit BEGIN {...} block. In other words, in case there's any initialisation in the module, that code might be executed before $::version is ready.  For example, this would not work as one might expect at first:

    #!/usr/bin/perl use strict; use warnings; $::version = shift @ARGV; BEGIN { # emulate "use Arithmetic;" package Arithmetic; sub load_config { my $config = "/usr/local/myapp/$::version/properties"; print "config: $config\n"; # ... } load_config(); }

    In case you recognise any similarities to your scenario, you could try wrapping the assignment $::version = shift @ARGV; in a BEGIN block, which would make sure that things are being executed in the proper sequence.

Re^3: Using variables in top level script inside the perl modules
by GrandFather (Saint) on Nov 06, 2008 at 09:37 UTC

    Pass the variable into the constructor for the object that needs it then. Besides, a little refactoring up front to add such a facility rather than hacking it in is almost always worth while.


    Perl reduces RSI - it saves typing