Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: single instance shared with module

by samurai (Monk)
on Sep 16, 2002 at 20:53 UTC ( #198370=note: print w/ replies, xml ) Need Help??


in reply to single instance shared with module

I believe, if I'm not mistaken, that this is often referred to as a singleton. I believe you can do something like so:

package A; my $obj = undef; sub new { my $class = shift; unless (ref $obj eq 'A') { # initilize object... $obj = bless \%self, $class; } return $obj; }
Now both main and B can call the same instance of A by just:

use A; my $obj = new A();
... in theory :) This is UNTESTED code and someone is allowed to thwack me in the back of the head soundly if I am missing something here.

--
perl: code of the samurai


Comment on Re: single instance shared with module
Select or Download Code
Re: Re: single instance shared with module
by sauoq (Abbot) on Sep 16, 2002 at 21:12 UTC

    A singleton class makes sense only if you never want more than one instance of the class to exist. If you just need to share an instance, there is nothing stopping you from doing so and allowing multiple instances if you need them.

    -sauoq
    "My two cents aren't worth a dime.";
    
Re^2: single instance shared with module
by adrianh (Chancellor) on Sep 16, 2002 at 22:52 UTC

    Be careful when using singletons. They are often a sign that you need to rethink a design. Then can encourage close-coupling of objects and become global variables by another name.

    There is a nice developerworks article on the subject. Even if you can't read Java it's worth a once over.

    That said - logging objects is one of the places that it's probably justified.... so I guess I'll just shutup and go away ;-)

Re^2: single instance shared with module
by Aristotle (Chancellor) on Sep 17, 2002 at 10:25 UTC
    A singleton is even easier to do in Perl. You don't even need a reference. Because in Perl you can say
    my $class = "Singleton"; $class->method();
    you only need to return a string with the package name in a singleton's constructor.
    package Singleton; my ($various, %instance, @variables); sub new { my $class = shift; # object initialization here ... *new = sub { shift } return $class; } 1;

    Here we also avoid the need to check whether the object is initialized by using a glob to replace &new. None of the further calls to the constructor will execute its initial code.

    All of your methods can just ignore the $self too, since there's a single instance which stores its data in file lexicals.

    Makeshifts last the longest.

Re^2: single instance shared with module
by adrianh (Chancellor) on Sep 17, 2002 at 21:28 UTC

    If you do want a singleton you might also want to consider using Class::Singleton. It makes the fact that you want a singleton nice and explicit, and allows sub-classing if necessary.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2014-10-21 08:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (98 votes), past polls