You have two solutions to your problem. Neither is preferable to the other - both work equally well.
- Catalyst and Excel::Template both solve this problem by passing the context around to every method. In E::T, this is an actual Excel::Template::Context object. In Catalyst, this is a Catalyst::Controller object. So, every method in both starts as so:
sub floober {
my ($self, $c) = @_;
}
- DBM::Deep prefers to provide a reference to the context in the instantiation of every object, then weaken it. So, every object can call $self->engine and get the context object. Thus, every constructor looks like:
sub new {
my $class = shift;
my ($params) = @_;
my $self = bless {}, $class;
$self->{engine} = $params->{engine};
Scalar::Util::weaken( $self->{engine} );
return $self;
}
As I'm the author of both
Excel::Template and DBM::Deep, I feel that each is useful in different situations.
BUT ... you really need to use one of them. Messing around with globals is bad juju. That you're asking here is a sign that you feel it smells wrong. That's the sign of a software journeyman. To demonstrate mastery, you need to learn why it smells and how to avoid it.
My criteria for good software:
- Does it work?
- Can someone else come in, make a change, and be reasonably certain no bugs were introduced?