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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.