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

Modules and DBI

by Angel (Friar)
on Nov 10, 2002 at 18:59 UTC ( [id://211775]=perlquestion: print w/replies, xml ) Need Help??

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

I have witten a state management system ( I know there is one on cpan ) but this is what ive got. Anyway I am using mySql to store that data and as cut and paste into the main module it works file. As a seperate module how do I pass the DBI handle so that the queries suceed?

Replies are listed 'Best First'.
Re: Modules and DBI
by Revelation (Deacon) on Nov 10, 2002 at 20:55 UTC
    If you're asking 'how can I pass around a database handle, so I don't have to generate it repeatedly,' there are a couple ways:

    The simplest of them would be to create a singleton class, which holds the database handle like so:
    package MyStateManagement::DBI::Singleton; use base qw(DBI); use DBI; my $Dbh = undef; sub connect { my $class = shift; $Dbh ||= DBI->connect( @_ ); return $Dbh; } 1;
    And connect by doing  MyStateManagement::DBI::Singleton->connect(); the same way you would connect with DBI. Assuming you use one database handle for the whole package, subsequent connects will use the same connection, and will not regenerate the connect; however, you could change the singleton class to check to see if the connect string was the same, and create some sort of connect hash, if there were multiple connections (or you could just specify the connection by a number, or something of that sort.)

    The alternative would be to just pass around your database handle through the subroutines.

    Gyan Kapur
    gyan.kapur@rhhllp.com
Re: Modules and DBI
by FamousLongAgo (Friar) on Nov 10, 2002 at 19:23 UTC
    Could you give us some code to look at? Persistence is a tricky topic and depends a lot on your particular needs - knowing the details will make for better advice. Also, is your decision not to use existing CPAN modules based on a desire to go with what you already have, or is there a feature that you haven't been able to find?

    As a pure guess, I'd say you might want to tie a variable ( so storage is transparent ), or use a persistence class that creates and keeps its own database handle. If you show us your code, we can give more useful suggestions.

    Good luck!

Re: Modules and DBI
by graff (Chancellor) on Nov 10, 2002 at 19:21 UTC
    I don't quite get your question. What have you tried that doesn't work, and what do you know about the nature of the failure?

    A DBI handle is just a reference to an object, stored in a scalar variable. Pass the scalar variable and you pass the reference to the object (i.e. the DBI handle). If this runs contrary to your findings, tell us more about your findings.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://211775]
Approved by ybiC
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (1)
As of 2024-04-25 04:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found