Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Greetings fellow monks,

I'm playing around with Class::DBI again, and I'd really like to pass dynamic DSNs and other database connection information to my parent class from the calling script or application module. I've been using Config::IniFiles for a while now, and I really like it and would like to continue to use it. I've been toying with a few ideas on how to combine my various wants, and here's what I've come up with.

#!/usr/bin/perl use strict; use warnings; use Config::IniFiles; my $cfg; BEGIN { $cfg = new Config::IniFiles( -file => 'settings.ini' ); $ENV{_MyCDBI} = join("\t", $cfg->val('Database', 'dsn'), $cfg->val('Database', 'Username'), $cfg->val('Database', 'Password') ); } use MyCDBI;

I'm using use here just because it's habit and because it runs parallel to using other CPAN stuff and my own application modules. However, I could just as easily require instead of use to simplify things.

my $cfg = new Config::IniFiles( -file => 'settings.ini' ); $ENV{_MyCDBI} = join("\t", $cfg->val('Database', 'dsn'), $cfg->val('Database', 'Username'), $cfg->val('Database', 'Password') ); require MyCDBI;

Either way, I just split the various bits inside my Class::DBI parent.

package MyCDBI; use base 'Class::DBI::mysql'; __PACKAGE__->connection(split("\t", $ENV{_MyCDBI}));

Now, this works just fine, and I'm more or less happy with it for my purposes, but it's far from optimum. First of all, I'm using poor-man's serialization of the bits for the database connection. A better solution would include the capability of sending stuff like {AutoCommit => 1} and whatever else. Plus, the code in the calling script is somewhat obnoxious. I'd love to use use if possible, but even without, it's three lines long. I know if merlyn saw this, he'd cringe and use a one-liner alternative.

So my question is thus: What are some better ways of doing this? (Better = less lines of code, more flexible data sent, more class encapsulation, more coffee, etc.) One thing I was thinking about was writing an abstraction class for my parent Class::DBI class. I've always hated having to refer to the subclass names multiple times.

my $obj = MyCDBI::SomeDBObj->retrieve(1); # versus my $cls = new MyCDBI; my $obj = $cls->SomeDBObj->retrieve(1);

But I can't really say exactly why I don't like this.

I've been reading about Class::DBI::Factory, but I have to admit that much of it goes way over my head. Should I just grin and deal with the learning curve? Or is there a simpler way?

gryphon
code('Perl') || die;


In reply to Abstracting Class::DBI Database Connection Data by gryphon

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-19 06:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found