You could just put it in package main, as in $main::db = new DB();, although it's one of those design choices that can get very deeply sedimented in a large project, and hard to change later.
If you're feeling amibitious, why not try a singleton class, with a class method that returns the data you need?
package GlobalVarsSingleton;
my $instance;
# a 'private' ctor
sub _new {
# init stuff
}
sub Instance {
my ($class, %args) = @_;
unless (defined $instance) {
$instance = $class->_new(%args);
return $instance;
} else {
return $instance;
}
}
The singleton ensures that you'll only have one instance of the data per interpreter, and the calling code doesn't have to care where that data lives.
|