[Techtalk] Perl script how to

Conor Daly c.daly at met.ie
Tue Aug 29 13:28:46 UTC 2006


On Tue, Aug 29, 2006 at 08:30:43AM -0400 or thereabouts, Helen wrote:
> Thank you for the clarification. I will try that a bit later.  This is what happens when a non programmer is trying to write a program.
> 
> Helen
>   ----- Original Message ----- 
>   From: Tamara Harpster 
>   To: Helen ; techtalk at linuxchix.org 
>   Sent: Monday, August 28, 2006 4:34 PM
>   Subject: Re: [Techtalk] Perl script how to
> 
> 
>   Let me see if I can explain a little more on this. You can have the
>   script run at the command line and still not have it activated by the
>   form when the HTML page calls it. And the library I am referring to is a
>   CGI library that allows reading the variables from the web server, for
>   example, the value of the select option that is sent. 

We use 'uncgi' http://www.midwinter.com/~koreth/uncgi.html .  It's a program
that translates the POST / GET values to environment variables.  Then your
script uses $WWW_foo where you had an <input type="text" name="foo"> in your
HTML form.  You use <form action="/cgi-bin/uncgi/your_script"...> where your
script is stored in /cgi-bin/your_script .
 
Here's a sample of one of our webform/cgi script units.  sql.cgi is a csh
script but it could be anything that can read / process the $WWW_...
environment variables.  Within that you could process your expect scripts
and reformat their output for display as html.  This script uses 'grep' to
filter the returned data and 'sed' to reformat it as an HTML table.

Conor

################################# runsql.html ############################
<HTML>
<HEAD><TITLE>Climate Database</TITLE>
<BODY>
<H1>Run a SQL Query
</H1>
Enter some SQL in the box below and click on the <B>RUN QUERY</B> button to
run it. Do not enter a final semicolon or a commit command.
<P>
<FORM ACTION="/cgi-bin/uncgi/sql.cgi" METHOD="GET">
<TEXTAREA ROWS=10 COLS=60 NAME="sqlin">
</TEXTAREA>
<INPUT TYPE="submit" VALUE="RUN QUERY">
</FORM>
<br>
<h2> Please use this tool with care, make sure that the sql statement is
correct and please don't run too large a request as it could dramatically
slow down server performance and also could hang your pc. </h2>

<br>
<br>

<h2> <A
 target=_blank 
  href="/sel_tables.html">Documentation on the main tables in the Climat
Database </A> this is just a sample link</h2>
<br>

</P>
</BODY>
</HEAD>
</HTML>
############################ end runsql.html ############################

################################# sql.cgi ############################
#!/bin/csh

# Get local environment variables
source /home/cliwww/.cshrc

# uncgi turns all the webform entities into $WWW_<entity_name> variables

# We need to treat a 'help...' command differently
@ ans = `echo $WWW_sqlin | grep help | wc -l `

if ( $ans > 0 ) goto hh # 'help...' command

# Tell browser it's getting HTML
echo "Content-type: text/HTML"
echo ""

# Test for forbidden keywords
# No 'delete...'
@ deletecount = `echo $WWW_sqlin | grep delete | wc -l `
if ($deletecount > 0 ) then
  echo " Sorry! only selects are allowed through the web"
  exit -1
endif

# No 'insert...'
@ insertcount = `echo $WWW_sqlin | grep insert | wc -l `
if ($insertcount > 0 ) then
  echo " Sorry! only selects are allowed through the web"
  exit -1
endif

# No 'update...'
@ updatecount = `echo $WWW_sqlin | grep update | wc -l `
if ($updatecount > 0 ) then
  echo " Sorry! only selects are allowed through the web"
  exit -1
endif

# Display the query received
echo "<TABLE border bgcolor=yellow>"
echo "<TR align=left><TD>Your SQL was</TD></TR><TR><TD>"
echo "$WWW_sqlin"
echo "</TD></TR></TABLE>"

# And the results 
echo "Results"

# In a table
echo "<TABLE border bgcolor=wheat>"

# Run the query and reformat the output to become a html table
# Note that this is a 'here' document in the form:
#
# command << LABEL
# input1
# input2
# input3
# LABEL
#
# 'input's 1-3 are passed into the program when it is run.
sql nu::climat <<FIN | grep '^|' | sed 's/|/<TR align=right><TD>/' | \
                         sed 's/|/<\/TD><TD>/g' |\
                         sed 's/<TD>$/<\/TR>/'
$WWW_sqlin
;
commit;
\g
FIN

# end of table
echo '</TABLE>'

# and quit
exit 0

hh: # If we get here, we got a 'help...' query

# The output is already suitably formatted so just send it out as plain text
echo "Content-type: text/plain"
echo ""

# Another 'here' document.  No formatting so just run it...
sql nu::climat <<FIN
$WWW_sqlin
;
commit;
\g
FIN
############################ end sql.cgi ############################
-- 
Conor Daly,                   
Met Eireann, Glasnevin Hill,  
Dublin 9, Ireland             
Ph +3531 8064276 Fax +3531 8064247

*********************************************************************************
This e-mail and any files transmitted with it are confidential and intended solely for the addressee. If you have received this email in error please notify the sender.
This e-mail message has also been scanned for the presence of computer viruses.

Ta an riomhphost seo, agus aon chomhad ata nasctha leis, faoi run agus is don te a seoladh chuige amhain e. Ma tharla go bhfuair tu an riomhphost seo tri dhearmad cuir in iul don te a sheol e led' thoil.

Ta an teachtaireacht riomhphoist seo scuabtha le bogearrai frithvireas.
********************************************************************************
NorthBridge Mail Server id e9yrhg43fncvj3974vjenw



More information about the Techtalk mailing list