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

I don't want to pass this variable from foo.pl

Why not? By accessing or manipulating the variable as a global variable it can be very difficult to determine where the value comes from and when or why it changes. By explicitly passing the value in as an argument to a sub or constructor it is much easier to manage and debug the code relating to it.


Perl reduces RSI - it saves typing
  • Comment on Re: Using variables in top level script inside the perl modules

Replies are listed 'Best First'.
Re^2: Using variables in top level script inside the perl modules
by alih110 (Novice) on Nov 06, 2008 at 09:32 UTC
    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.

      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.

      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