Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I might be doing something very stupid. I thought I will seek a bit of wisdom.

I had done a little bit of C++ coding. The concept of packages/modules were not there in C++ then. And not having done some reading on Perl first, I had begun to implement objects that I needed only one instance of as singletons even if the objects consisted of only methods and no data.

I am still not very familiar with the package/module concepts. But reading a bit, I get the feeling that I should be using modules rather than the singletons.

Just need a bit of an advice to get on the right track.

Thanks

Replies are listed 'Best First'.
Re: Module vs Singleton
by locked_user sundialsvc4 (Abbot) on Jul 15, 2011 at 21:33 UTC

    Singletons are easier than you might suppose to do in Perl ... but first, you’re going to have to “un-learn” some of your C++.

    Perl objects live in packages.   Yet, there is nothing special about them, except that they contain a subroutine which by convention is named new, and which contains something along the order of the following:

    my $class = shift;
    my $self = {};
    bless $self, $class;
    return $self;

    Now, what this code is doing is to instantiate a hashref and then to bless it, which tells Perl that this hash (it could be some other kind of data-structure) is to behave like an object.   Then, the subroutine returns that hashref.   Since the hashref is being returned, it isn’t going out of scope so it isn’t destroyed by the garbage-collector.

    So, how would you create a singleton?   Through the use of a package variable.   Like this:

    my $singleton = undef; sub new { my $class = shift; unless (defined($singleton)) { $singleton = {}; bless $singleton, $class; } return $singleton; }

    Now, only the first call to new will initiate a new object.   Every successive call will simply return the same one.

    When you are coming from a highly-strictured language like C++, working with Perl can sometimes feel like barnstorming.   “Now that’s flying...!”

      What you have explained here clears my understanding.

      Actually, I had searched around and found Class::Singleton and had followed the documentation in that to work with the singleton objects. I was deriving from Class::Singleton. What you have explained is perhaps a simpler way of doing it without requiring the inheritance.

      Also, it had never occurred to me that I could use modules for some of these instead of Singletons.

      For example, if I had a set of utility functions, I created a Singleton class for it where a module perhaps would have been more appropriate.

      It works. But perhaps it is not the right way and less efficient. Should I go back to clean this up and convert some of the Singletons to modules?

        There's nothing wrong with singletons. This would be a good time to explore Moose, especially MooseX::Singleton. Following up on Anonymous Monk's "new", here's what I tried:
        package Foo; use MooseX::Singleton; has stuff => ( is => 'rw', isa => 'HashRef[Str]', default => sub { { default => 'new' } }, ); package main; my $instance = Foo->instance; my $same = Foo->instance; use Data::Dumper::Concise; print Dumper($same);

        Oh, no, no ... if you have found a CPAN module to do what you are looking for, I am always in favor of going that route, because doing so usually gives you a more-robust and well-thought-out solution with less work.   Those CPAN contributors are gooood ...

        Nevermind any concerns for the computer’s “efficiency.”   (At however-many billion operations per second, no one can hear you scream ...)   Rather, think only about your own.   What is the easiest and most reliable to build ... the most serviceable and maintainable ... the most obvious, both now and six months from now when your own code has become the work of a perfect stranger.   (How soon we forget, especially as we get old... aww, forget it...)   ;-)   If you have already put your hand to the plow, it’s rarely a good idea to look back.

        You will soon hear around here this familiar mantra:   TMTOWTDI.™   (a.k.a. “Tim Toady.”)   And you will soon know why we say it.

Re: Module vs Singleton
by Anonymous Monk on Jul 15, 2011 at 17:10 UTC

      Just wanted to clarify this–

      package Foo; sub new {} # Foo::new, Foo->new

      Those calls to new are not equivalent–

      ~>perl -lwe 'BEGIN{ package Foo; sub new { print shift } } Foo->new; F +oo::new' Foo Use of uninitialized value in print at -e line 1.