APM: Running a perl prog from a perl prog?

Brent LaVelle brentlavelle at yahoo.com
Tue Oct 17 19:06:36 PDT 2006


Not using DBI is  generally a bad idea.  Look at Mason, you can content wrap files  with sql in them and the wrappers will execute the sql and format the  results.  Look at http://www.masonbook.com/book/chapter-5.mhtml  and search for "An Advanced Inheritance Example"  I left the world  of Perl/CGI for SW test before I even used Mason for a real project so  I cannot endorse it but it may be what you are looking for.  I was  sold on content wrapping after reading the O'Reilly book on Mason.

----- Original Message ----
From: CaptNemo <CaptNemo at Austin.rr.com>
To: austin at pm.org
Sent: Tuesday, October 17, 2006 6:26:24 PM
Subject: Re: APM: Running a perl prog from a perl prog?

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.

_______________________________________________
Austin mailing list
Austin at pm.org
http://mail.pm.org/mailman/listinfo/austin







More information about the Austin mailing list