bschmer has asked for the wisdom of the Perl Monks concerning the following question:

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?

Replies are listed 'Best First'.
Re: Set attribute on a module
by dragonchild (Archbishop) on Apr 14, 2006 at 18:25 UTC
    The first method you mentioned is the canonical one. However, if you must do it on import, I would organize it as so:
    package My::Singleton; my $object = bless {}, __PACKAGE__; sub import { shift; my %args = @_; $object->$_( $args{$_} ) for keys %args; return; } sub new { return $object } # Your methods here

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?