Esteemed Monks, I am making some changes to a module that implements a singleton object that can be used by other modules. I'm trying to set a global configuration variable before the module in question can be used by other modules so that they won't be using the wrong value for the configuration variable. While having the code reviewed at work one of my coworkers commented that the following was a "nasty hack":
use Module; BEGIN { $Module::variable = 1; };
Since the module is an object, the preferred style of my coworker was:
use Module; BEGIN { my $m = new Module; $m->setConfigVariable(); };
Neither of us is horribly thrilled with the BEGIN block and would rather have something like:
use Module qw(-SetConfigVariable);
I've been poking around for modules that do something similar, but haven't come across anything. I've coded up a generic solution that mostly works using import but the variable gets reset on exiting import if I don't reference the real variable in import.
my $MyConfigVar=0; sub import { # Import gets called with a package and a list of items # We want to pick off the ones that start with - and # handle. Other cases need to be handled as they are # encountered my $package = shift; $Trace = new TracePkg unless defined($Trace); $Trace->entry(); my @params = @_; foreach my $item (@params){ if ($item =~ s/^-//){ my $varname = $item; $varname =~ s/^no_//i; my $oldvalue = eval "\$$varname"; if ($item =~ /^no_/i){ eval "\$$varname = 0"; } else { eval "\$$varname = 1"; } my $newvalue = eval "\$$varname"; $Trace->comment(msg => "Handled param $varname. Changed from $o +ldvalue to $newvalue."); } else { $Trace->warn(msg => "Did not handle param $item"); } } # I haven't figured out why we need this statement but commenting it + out will cause # the variable to get reset. $Trace->trace(msg => "Var=$MyConfigVar"); $Trace->exit(); }
I'm wondering: Am I crazy for wanting to do something like this? Is there a better way?

In reply to Set attribute on a module by bschmer

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.