Spreading your code out among several different files won't really clean up your code ... it will just spread it out among several different files. :)

What you need to do is search the CPAN for some modules that will make your task easier ... modules like DBIx::Connect which will allow you to abstract the DB connect info out of your scripts (and into a config file). However, before i start ripping my one script into a modularized, maintainable package ... i'd clean up what you have. Here is how i would code your snippet:

my $dbh = DBI->connect( qw(DBI:Oracle: user pass), {RaiseError => 1}, ); # with RaiseError turned on, no need for die and errstr $dbh->do("truncate table directory_data"); my $sth = $dbh->prepare(q{ INSERT INTO directory_data( dn, cn, surname, given_name, initials, generational_qualifier, building, room_number, mailstop, telephone, fax, mail, tfs_id, uid ) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?) }); $sth->execute($employee{$_},.,....) for keys %employee;
Sometimes i like to use code to generate an INSERT statement:
my @field = qw( dn cn surname given_name initials generational_qualifier building room_number mailstop telephone fax mail tfs_id uid ); my $table = 'directory_data'; my $sql = "insert into $table (" . join(',', @field) . ') values (' . join(',', map '?', @field) . ')' ;
You could turn that snippet into a subroutine and pass it the name of the table, an reference to an array of field (column) names, and a reference to another array that contains the values (just make sure the two arrays line up!).

Hope this helps and gives you some ideas. :)

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

In reply to (jeffa) 3Re: modularizing database script? by jeffa
in thread modularizing database script? by Anonymous Monk

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.