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

if i make a singleton object, and it returns a reference to a global variable within it- that can be changed without using the object's methods... is it defeating the whole purpose of using poop ?

i just started playing with poop. and it's kind of weird for me. grandfather sugested i use it in my quest of having global vars in a project- but i wanted to uhm.. not be too messy with the symbol tables, and i read that globals are bad juju.

i have this hash %FRIDGE and it has stuff in it, i want to put and take stuff out of it, and any module code that uses House/Kitchen/Fridge.pm to use the same fridge and see the same data that other code may have modified.

there are lots of ways code may want to take a banana out of the fridge, and i want to allow other people to easily implent ways of putting stuff in

Replies are listed 'Best First'.
Re: poop question
by friedo (Prior) on Feb 21, 2006 at 22:35 UTC
    You can avoid the use of package globals in a singleton object by using a lexical. For instance:
    package My::Singleton; my $DATA; sub get_data { my $class = shift; $class->_init unless defined $DATA; return bless $DATA, $class; } sub _init { $DATA = { foo => 42 }; }
    Because $DATA is a lexical ("my") variable, it's only accessable to the subroutines defined in its scope. (In this case, a file scope, but you could add a bare block to hide it from other parts of the same file if you really want to.) See also Class::Singleton.