APM: Running a perl prog from a perl prog?
CaptNemo
CaptNemo at Austin.rr.com
Tue Oct 17 16:26:24 PDT 2006
What I'm trying to do is write a web prog so that people can search a
database and easily pull the info.
I HATE dealing with DBI so I wanted to make the database stuff modular. I
want the main prog to handle the cgi stuff and call smaller progs to do the
database stuff. That way I can re-use the modules to make more progs.
HERE'S MY FIRST MODULE:
dbquery.pl
--------------------------------------------------------------------------------
#!/usr/bin/perl -wT
use DBI;
use strict;
my $dbdriver = $ARGV[0];
my $db = $ARGV[1];
my $dbuser = $ARGV[2];
my $dbpasswd = $ARGV[3];
my $dbtable = $ARGV[4];
my $condition = $ARGV[5];
my @row_array;
if ( $#ARGV ==5 ) {
my $dbh = DBI->connect( "DBI:$dbdriver:$db",$dbuser,$dbpasswd,)
|| die "Database connection not made: $DBI::errstr";
my $sth = $dbh->prepare("SELECT * FROM $dbtable WHERE $condition")
or die $dbh->errstr;
$sth->execute() or die $dbh->errstr;
while (my @row_array = $sth->fetchrow_array) {
print "$row_array[0], $row_array[1], $row_array[2] \n";
}
$dbh->disconnect();
}
elsif ( $#ARGV <= 4 ) {
print "\n-----------------------------ERROR------------------------------\n";
print "NOT ENOUGH ARGUMENTS\n";
print "dbquery.pl usage: dbquery.pl DBDRIVER DB DBUSER DBPASSWORD DBTABLE
\"CONDITION\"\n";
print "NOTE: DBDIVER is usually \"mysql\" but it can be other things like
\"oracle\".\n";
print " EXAMPLE: dbquery.pl mysql databas...\n";
print "NOTE: The total CONDITION must be in quotes with the matching data
in single quotes. \n";
print " EXAMPLE: \"product LIKE 'bicycle'\"\n";
print "---------------------------END ERROR----------------------------\n\n";
}
else {
print "\n-----------------------------ERROR------------------------------\n";
print "TOO MANY ARGUMENTS\n";
print "dbquery.pl usage: dbquery.pl DBDRIVER DB DBUSER DBPASSWORD DBTABLE
\"CONDITION\"\n";
print "NOTE: DBDIVER is usually \"mysql\" but it can be other things like
\"oracle\".\n";
print " EXAMPLE: dbquery.pl mysql databas...\n";
print "NOTE: The total CONDITION must be in quotes with the matching data
in single quotes. \n";
print " EXAMPLE: \"product LIKE 'bicycle'\"\n";
print "---------------------------END ERROR----------------------------\n\n";
}
It queries a database and returns the first 3 records of matches.
More information about the Austin
mailing list