SPUG: cgi script template

Joe Devlin jdevlin at stadiumdistrict.com
Tue Jun 27 11:49:22 CDT 2000


This is a shell of a functioning script.  
There are subroutines in Common.pm that we use
to make are lives easier, but are not included
here for brevity.


#!/usr/bin/perl -w

# # # # # # # # # # # # # # # # # # # # # # # # # # # # 
# File:		myquery.pl
# Written by:	Devlin Technical Consulting
#			Tacoma, WA
#
# Customer:	xxx
# 			xxx
#
# Description:
# This query accepts information from the
# forms ........
#
# It enters the data into tables xxxx ......
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # 
# Revision 1	3/5/00		Initial

# Initialize some modules
	use DBI;			#datbase interface
	use CGI qw(:all);	#creating web pages
	use CGI param;		#or this for getting parameters
	use English;
	use diagnostics;	#verbose failure codes
	use Common;		#Devlin Technical Consulting common commands
	use strict;		#must declare global variables
					#here are the globals
	use vars qw( $action $item $dummy $mstate $i $n %form_data
			$form_data $document_root %fields $path
			$filetoopen $dsn $user $password $server_name 
			$query_string);


# Initialize some variables
	$mstate=param("mstate");#state machine design variable
	$dummy="";			#dummy return value for debug

# # # # # # # # # # # # # # # # # # # # # # # # # # # # 
#Main part of program

	####################################
	# get the data passed from the form
	#get the input from the form by either method
		if ($ENV{'REQUEST_METHOD'} eq "GET") { 
			$query_string = $ENV{'QUERY_STRING'};
		} 
		elsif ($ENV{'REQUEST_METHOD'} eq "POST") {
			read (STDIN, $query_string, $ENV{'CONTENT_LENGTH'});
		}
		else {
			# Offline mode goes here.
		}
	%form_data=form_to_hash($query_string);


	####################################
	# get the directory from which web documents are served
	$document_root = $ENV{'DOCUMENT_ROOT'};

	####################################
	# get the server name hosting this site
	$server_name = $ENV{'SERVER_NAME'};

# # # # # # # # # # # # # # # # # # # # # # # # # # # # 
#Figure out what form (or mstate) just called this script
	# submission of form query by users parameters, 
	if ($mstate eq 'query_1')
	{
	&pp_query_1();
	}
	# + + +  + + +  + + +  + + +  + + +  + + + 

exit(0);

#Define subroutines
# + + +  + + +  + + +  + + +  + + +  + + +  + + +  + + +  + + #
# + + +  + + +  + + +  + + +  + + +  + + +  + + +  + + +  + + #
sub pp_query_1{
	#Define some local variables
		my $dbh= my $sth= my $ary_ref= my $account_number= my $query="";
		
	#Pre-process the form data
		#highlight video checked must be Y(es) or N(o) for comparison to the databse

		unless($form_data{'myUsersChoice'} eq "Y"){
			$form_data{'myUsersChoice'} = "N";
		}

	#Get  login, password, and datasource name
		&get_login_pass_dsn;

	#Access the Database
		#my $trace_level=3;#debug only
		#my $h = DBI->trace($trace_level);#debug only

		$dbh = DBI->connect($dsn, $user, $password,
        	                   { RaiseError => 0, PrintError=> 1})
		or &bail_out("Cannot connect to database");

		#issue query: get the account number of the first result
		$query=qq[

			SELECT 
				table1.field1
			FROM 
				table2,
				table3,
				table4
			WHERE 
			table.a_field = "$form_data{'a_field'}"];

		$sth = $dbh-> prepare ($query)
			or &bail_out("Cannot prepare query");
		$sth->execute ()
			or &bail_out("Cannot execute query");

		#read the results of the query: account number of first match
		$account_number = $sth->fetchrow_array();

		#Check the results
		if ($account_number==undef)
		{&bail_out("No results found during retrieval.");}

		$sth->finish ()
			or &bail_out("Cannot finish query");

		$dbh->disconnect
			or &bail_out("Cannot disconnect from database");

	#okay the query was successful, display the results
	&print_frameset($account_number);

}# end of sub pp_query_1
# + + +  + + +  + + +  + + +  + + +  + + +  + + +  + + +  + + #
# + + +  + + +  + + +  + + +  + + +  + + +  + + +  + + +  + + #


# + + +  + + +  + + +  + + +  + + +  + + +  + + +  + + +  + + #
# + + +  + + +  + + +  + + +  + + +  + + +  + + +  + + +  + + #
sub bail_out{
	my $message = shift;
	my $filetoopen = "/search/query_fail_template.html";

	%fields = (
		error_message=>"$message"
		);
	print "Content-type: text/html\n\n";#must preceed print template command
	print template( "$document_root" . "$filetoopen", \%fields);
	die "Bailed out ....";
}
# + + +  + + +  + + +  + + +  + + +  + + +  + + +  + + +  + + #
# + + +  + + +  + + +  + + +  + + +  + + +  + + +  + + +  + + #
#




----------
From: 	jeff[SMTP:jeff at planetoid.net]
Sent: 	Monday, June 26, 2000 2:51 PM
Cc: 	spug-list at pm.org
Subject: 	SPUG: cgi script template 

is the following a good template for develping perl/cgi/db applications?

#!/usr/bin/perl

use stuff/config

eval {
    connect to db
    do some stuff
};

if ($@)
    error reporting
}

exit 0;

or does defining a sig die handler improve readability or exception
handling...

jeff at planetoid.net



 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
 Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
 For Subscriptions, Email to majordomo at pm.org:  ACTION  spug-list  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email address





 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
 Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
 For Subscriptions, Email to majordomo at pm.org:  ACTION  spug-list  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email address





More information about the spug-list mailing list