in reply to Re: Design of a global-variable storage package
in thread Design of a global-variable storage package

You both appear to have overlooked the very simple alternative:

my $p= 'Getopt::Long::Framework'; $p->What(); $p->Ever();

But I don't buy the 'singleton' concept in general. Even CGI.pm realizes that sometimes There Can Be More Than One, even for things where that is almost never wanted and it is easy to fail to see any cases when someone would want Two.

Implement your functionality using plain (non-singleton) objects and just provide a handy shortcut for getting the default, global instance. Then you have the benefits of 'singleton' without the limitations.

- tye        

Replies are listed 'Best First'.
Re^3: Design of a global-variable storage package (string)
by Tanktalus (Canon) on Nov 23, 2005 at 17:55 UTC

    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?

      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