Hi,
I thought I'd share my Q module for CGI programming. It's probably pretty
crude, but it responds to POST or GET data (fail-over from former to
latter), decodes CGI strings correctly, and allows access to any/all
key/value parameters passed to a script.
Optional things: the sqlXML function requires ODBC. If you don't have it,
remove the module from the import line and don't call sqlXML. Otherwise, it
takes a handle and a query-string (assumes no parameters).
Function description:
public condstr S, // conditionally wraps expression with `str'
starttag T, // makes a start-tag, "<T>"
endtag T, // makes an end-taag, "</T>"
cgipairs, // hash of all CGI pairs
tag T C, // puts content C in start/end tags T
tagify, // legacy name for tag
test, // use for debugging installation
xmlify A B, // builds XML from pair-list B, using A as nodenames
cgivar S, // returns value of CGI parameter named S
sqlXML H Q; // takes handle, SQL; returns XML for later XSLT
Examples:
a) testing
#!/usr/bin/env q
#! -cmain ARGS || quit
import cgi;
main = cgi::test;
this dumps a minimal HTML page with title, showing the keys found - so
invoke it as index.q?foo=bar.
b) building XML from a SQL query for later transformation - note this
should be rewritten for Q 5.4 since it has its own XML and XSLT functions
now:
#!/usr/bin/env q
#! -cmain ARGS || quit
import cgi, odbc;
def H=odbc_connect "DSN=someODBCdsn";
main = writes (sqlXML H "select * from sometable;");
Please consider it released under the terms of the GPL, for inclusion in
some form in future versions of Q if you wish.
[[c0c2272509f29b1f91e5682c273dd0f0 cgi.q]]
Cheers,
~Tim
--
<http://spodzone.org.uk/>
|