Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Object singleton method

by sh1tn (Priest)
on Apr 21, 2008 at 22:35 UTC ( #682038=perlquestion: print w/replies, xml ) Need Help??
sh1tn has asked for the wisdom of the Perl Monks concerning the following question:

Fellow monks, is this possible? Thank you.

Replies are listed 'Best First'.
Re: Object singleton method
by pc88mxer (Vicar) on Apr 21, 2008 at 22:45 UTC
    Of course it's possible! There's even a module on CPAN to do it: Class::Singleton.

    However, a singleton is really more of a design pattern, so there's a lot of ways to implement it. Rolling your own isn't that hard:

    package MyClass; my $instance; sub get_instance { unless ($instance) { $instance = ...; # instantiate instance here } $instance; }
      The example is another pointer to Class singleton method and not instance one. All I wanted is:
      s = "s" def s.singleton print "ok" end # later on s.ok

Re: Object singleton method
by brian_d_foy (Abbot) on Apr 22, 2008 at 05:35 UTC
      Thank you, nice article. All the examples for singleton point to class methods and not instance ones. I wanted something like:
      my $obj1 = Class->new; my $obj2 = Class->new; # definition of singleton method for $obj1 goes here ... $obj1->singleton_method and print "It works.\n"; $obj2->singleton_method and print "It doesn't work.\n";

        I'm not sure what the benefit is of having objects that have the same class but have not all methods. Most likely, you could achieve what you want by overriding ->can and having the method fail for objects that are not the singleton object.

        Otherwise, you might want to look at classless objects:

        Oh, you're not looking for singleton objects. You're looking for objects of the same class to have the same method names but do different things based on the object. Singletons aren't about the method names but how many objects there actually are.

        You have a bit of an XY Problem here since we don't know what you are trying to solve. What are you trying to accomplish (rather than how you are trying to do it)?

        If you want the objects to start off as the same class and specialize later, look at Class::Prototyped. Each object can start the same, but then gain or replace methods (called "slots" in prototype programming). Randal's CGI::Prototype stufff is based on this and once you get used to the idea it's very nice to work with given an appropriate problem.

        Good luck, :)

        brian d foy <>
        Subscribe to The Perl Review

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://682038]
Approved by Old_Gray_Bear
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2018-07-16 00:48 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (330 votes). Check out past polls.