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

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

Replies are listed 'Best First'.
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.

    "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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://198370]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2018-06-18 05:16 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (108 votes). Check out past polls.