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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.