What I've standardized on for my database type applications is a series of object oriented modules that get extended by (for a lack of better term) child modules.

The Data Object

Let's start with the dataObj module. I mentioned in another thread that I keep my database and other generic information in another module and I'll hand wave that here.
package dataObj;

use dbConf;   # This module has the db connect info

my $connected=0;


# this rarely gets actually invoked.
sub new { 
   my $proto=shift;
   my $class = ref($proto)|| $proto;
   my $self = { dbh=> undef,
                sth=> undef
              ... and other goodies
              }

    bless $self,$class;

    $self->connect2db if not $connected; 

    return $self;
}

sub connect2db {
     my $self=shift;
     my $method=( $_[0] ? $_[0] : "oracle");

     my $config=new dbConfig($method);

     # The connectParms method of dbConfig returns an 
     # array with the DSN, LOGIN and PASSWORD info
     $self->{dbh} = DBI->connect($config->connectParms)
            or die "Could not connect " . $DBI::errstr;
}

sub prepare {
    my $self=shift;
    my $sql=shift;

    die "Preapre without connect!" if not $connected;
    $self->{sth}=$self->{dbh}->prepare($sql)
          or die "Could not prepare\n" . $sql . "\n"
                  . $self->{dbh}->errstr;
}

sub execute {
    my $self=shift;

    return if not $self->{sth};

    $self->{sth}->execute(@_);
}
.... and so on...

1;

CAVEAT:The above code is just to get the idea across and is not to be considered prime time code

I would then "overload" the module with the data object in question. Presumably I would put methods into the data object that deal with types and such intelligently

Example:

package dataObj::contactInfo;
use vars qw/ @ISA /;
use dataObj;
@ISA=qq(dataObj);

sub new {
    my $proto=shift;
    my $class=ref($proto)||$proto;
    my $self= {
               name => undef,
               email => undef
    };

     bless $self,$class;

     $self->connect2db("mysql1");

     return $self;
}

sub get_contact_by_name {
     my ($self,$name)=@_;

     return if not $name;
     $self->prepare("select email from contacts where name= ?");
     $self->execute($name);
     my $vals=$self->fetchrow_hashref
     ...blah...blah....blah...



1;

Hopefully you get the idea.


Peter L. BergholdBrewer of Belgian Ales
Peter@Berghold.Netwww.berghold.net
Unix Professional

In reply to Re: Code factory by blue_cowdawg
in thread Code factory by Notromda

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.