in reply to Re^2: Design of a global-variable storage package (string)
in thread Design of a global-variable storage package

I thought of that $p part, too, just forgot to mention it. I'm not sure, though, that this would be better-received. ;-)

As for your non-singleton approach - something like this?

my $default; sub new { my $class = shift || __PACKAGE__; my $self = {}; bless $self, $class; $default ||= $self; } sub global_instance { $default ||= (shift || __PACKAGE__)->new(); }
And, perhaps, a way to change the global instance to be another one, if you really do need More Than One?

Replies are listed 'Best First'.
Re^4: Design of a global-variable storage package (Two)
by tye (Sage) on Nov 23, 2005 at 21:11 UTC

    Rather than try to detect, I'd probably do something deterministic like export functions that use the singleton:

    package Getopt::Long::Framework; use vars qw( @EXPORT_OK ); @EXPORT_OK= qw( Foo Bar Baz ); require Getopt::Long::Framework::Object; my $singleton = Getopt::Long::Framework::Object->new(); sub Foo { $singleton->Foo( @_ ); } ... sub GetObjectPackage { "Getopt::Long::Framework::Object"; } sub new { my $class= shift @_; $class = $class->GetObjectPacket() if $class->can("GetObjectPacket"); ... }

    (in part, because I prefer to keep my class methods and object methods in separate 'package's anyway)

    - tye