G'day justsomeguy,

Here's some skeleton code that offers a general approach you could take. Also, given your request is a little on the vague side, I've provided a few alternative paths you may like to investigate.

Put your utilities in a module. I've used an OO module; a functional module may better suit your needs. I've shown hand-crafted constructor and accessor methods for demonstration purposes; you may prefer Moose (or similar). Also, I've used DBI (as indicated in your OP); other modules (e.g. DBIx::Class) may be a better choice for you.

package Your::SQL::Utils; use DBI; use DBD::Pg; sub new { my ($class, ...) = @_; bless { dbh => _connect() } => $class; } sub _connect { ... return $dbh; } sub dbh { my ($self) = @_; $self->{dbh}; } sub some_query { my ($self, ...) = @_; ... my $sth = $self->dbh->prepare(...); ... $sth->execute(...); ... } sub other_query { ... }

Your script might look something like this. (Here's the Getopt::Long documentation in case you're unfamiliar with this core module.)

# your_prog.pl use Your::SQL::Utils; use Getopt::Long; my $query; GetOptions('query=s' => \$query) or die "..."; my $util = Your::SQL::Utils::->new(); $util->$query();

Serving suggestion:

$ your_prog.pl -q some_query

-- Ken


In reply to Re: DBI Application Development by kcott
in thread DBI Application Development by justsomeguy

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.