Sub::SingletonBuilder - a tiny module to create singletons

tag

There are times when you want to make a certain object a singleton. It is not at all difficult to write a function that creates a singleton object (the example below wraps a DBI handle using the singleton pattern), however the code is not self-explanatory enough so in general you would need to add comments, write tests, and so on.

*dbh = sub {
    my $dbh;
    sub {
        unless ($dbh) {
            $dbh = DBI->connect(...)
                or die $DBI::errstr;
        }
        $dbh;
    }
}->();

That's when the tiny module Sub::SingletonBuilder comes in. As can be seen in the following code snippet the meaning of the code becomes clear and concise.

use Sub::SingletonBuilder;

*dbh = build_singleton(sub {
    DBI->connect(...)
        or die $DBI::errstr;
});