in reply to DBI Application Development

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