http://qs321.pair.com?node_id=110123


in reply to OO vs. global variables...

Or you could use a singleton, which as I understand it is a way of doing objects so that any attempt to create a new object actually returns a refernce to the first (and only object) created. This way you can access the same object without having to hand refs around.

In practise this isn't done much, because modules do the same thing with a lot less fuss. If you want global variables, just create a module with variables in it, and access the variables from your main program. This is one of the times you can really appreciate Perl letting you break the rules in a good way.

____________________
Jeremy
I didn't believe in evil until I dated it.

Replies are listed 'Best First'.
Re (tilly) 2: OO vs. global variables...
by tilly (Archbishop) on Sep 05, 2001 at 20:38 UTC
    But note that there are good reasons to use singletons in Perl. The most common one for me is when the object is expensive to create (database connections etc) and might need to exist for many reasons, but might be possible to do without. In that case then you just use a singleton and issue calls to its constructor from wherever you need it.

    Now no matter how complex the logic is to need it, it is there when you want, and you don't pay for it when you don't need it.