Let's say I'm using some module, Foo (which I did not write). At the top of the module, we see stuff like the following:
package Foo; $Foo::VERSION='3.02'; sub set_globals { $DEBUGGING = 0; $LOG_ERROR = 0; $ERR_MIN = 20; $DEFAULT_XFR = 'G0023'; } set_globals(); #... more code ...
This is actually an OO module that I need to use as a base class, but I want to make sure that I safely override those globals with my own values.
################################ package Bar; ################################ $VERSION = 1.0; use strict; use Foo; use vars qw/ @ISA /; @ISA = qw/ Foo /; INIT { $Foo::LOG_ERROR = 1; $Foo::ERR_MIN = 20; $Foo::DEFAULT_XFR = 'H00293'; } sub new { my ( $class, $args ) = @_; # etc...
Naturally, in my own code, I have:
use strict; use Foo::Bar; my $obj = Foo::Bar->new;
What I was wondering is whether or not I could alter the contents of those globals at the time that I use Foo::Bar. For instance, perhaps I want to set $Foo::LOG_ERROR to 0, I might want a statement like this:
use Foo::Bar qw/noLog/;
The above would ideally be able to change several globles to match whatever configuration is needed at run-time. To compound things, the base class, Foo, has subs that can be called as OO methods or functions, so I can't try any trickery in the new constructor as I cannot guarantee that it will be called.
I know this sounds a bit strange, but the author of Foo has requested that my subclass have a way to set those globals at load time. Any ideas welcome.
Cheers,
Ovid
Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.
In reply to Setting Globals in a Base Class by Ovid
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |