Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Perl Singletons

by sheridan3003 (Beadle)
on Jul 19, 2002 at 19:45 UTC ( #183417=perltutorial: print w/ replies, xml ) Need Help??

Perl Singleton Objects

This is a quick introduction to perl Singleton objects. A singleton object is an object that is only instantiated once throughout an application.

Singleton Uses

Singletons can be used for many things. If you only want one database connection for a particular application, or a limited number of connections a singleton database class can handle managing your open connections.

How to implement

The following is an example of how to put together a database object that opens a database connection and returns an existing connection to the caller.This allows your userid/password to be set in a single location as well. You don't have to have every piece of code that accesses the database use userid/password. Just use the DBConnector object and then issue your database calls.
package DBconnector; my $singleton; sub new { my $class = shift; $singleton ||= bless {}, $class; } sub connect { my $self = shift; if (exists $self->{dbh}) { $self->{dbh}; } else { my $connection = "dbi:mysql:host=$self->{host} \ dbname=$self->{name}"; $self->{dbh} = DBI->connect($connection, \ $self->{user},$self->{password}); } }

Summary

There are many ways to use singletons. This is just a basic example, but provides what is needed to create a singleton object.

Comment on Perl Singletons
Download Code
•Re: Perl Singletons
by merlyn (Sage) on Jul 19, 2002 at 19:52 UTC
    There's no point in even returning an instance. Just return the package name:
    package DBconnector; my $dbh; sub new { return __PACKAGE__ } sub connect { my $pointless = shift; $dbh ||= DBI->connect($@); }
    and so on.

    -- Randal L. Schwartz, Perl hacker

      $dbh ||= DBI->connect($@);

      You probably mean @_ there.. at least I can't see an eval anywhere *grin*

      ++ tho. :)

      Makeshifts last the longest.

Re: Perl Singletons
by gav^ (Curate) on Jul 19, 2002 at 23:16 UTC
    Instead of rolling your own DBI singletonn you might want to look at Ima::DBI.

    Class::Singleton is another useful module.

    gav^

Re: Perl Singletons
by atcroft (Monsignor) on Jul 23, 2002 at 17:16 UTC

    Although I was aware of the concept of such an object, I was unfamiliar with the term. Forgive my lack of knowledge in the ways of objects and OO, but isn't that similar to class values?

    You suggest that such an object can be used for limiting connections, for example, but did not mention how. Would you simply set a flag variable to say if or how many connections were in use? If that is so, how would you handle concurrent attempts, or attempt to prevent race conditions?

    My thanks to you for the instruction of your posting, as well also to those who commented.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (12)
As of 2014-12-26 06:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (165 votes), past polls