Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: OO vs. global variables...

by jepri (Parson)
on Sep 04, 2001 at 22:53 UTC ( #110123=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://110123]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2022-01-29 08:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (74 votes). Check out past polls.

    Notices?