CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Intermediate:
Calling subroutines to parse forms



Aug 30, 2000, 12:27 AM

Post #1 of 2 (908 views)
Calling subroutines to parse forms Can't Post

Good Morning all,

I wonder if anyone out there could help? I've created a script that prints an html form, then when I want to parse the form I call a subroutine, but it just doesn't appear to work.

I've added some very simple code here to display what I've got, is it difficult or am I missing something simple?

Here's the code, and you can see it in action here.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


# Call subroutine print_html

# Specifies the subroutine print_html
sub print_html{
print "Content-type: text/html\n\n";
print qq{
<title>Test Form Script</title>
<body bgcolor="#000000" text="#FFFFFF">
<form method="post" action="?get_info">
Select an option<br>
<input type="radio" name="select" value="Yes">
<input type="radio" name="select" value="No"><br>
<input type="Submit" name="Submit" value="Submit">

# Specifies the subroutine get_info
sub get_info{
use CGI;

# Specifies the subroutine check_it
sub check_it{
print "Content-type: text/html\n\n";
print "You selected $results";

Regards and TIA
Richard Smile

Dizzie Scripts


Aug 30, 2000, 4:19 AM

Post #2 of 2 (908 views)
Re: Calling subroutines to parse forms [In reply to] Can't Post

You can't just send a parameter to a CGI program and expect it to run a function of that name for you.

Oddly enough, this question came up in the CIWAC (comp.infosystems.www.authoring.cgi) newsgroup, and I had an answer for it. If you call your program as, then the following subroutine will execute the given function (if it exists).

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

# dispatch(CONTINUE)
# CONTINUE - boolean determining whether to
# continue running the program after the
# function has been called
sub dispatch {
my $continue = shift;
my $func = $ENV{QUERY_STRING};
my $glob = *$func;
$glob->() if defined &$glob;
exit unless $continue;

This function is used like so:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


dispatch($x); # where $x is true or false
# your program here...
sub dispatch {
# the dispatch function here...

Note that dispatch() makes no effort to URL-decode the query string. Should that be needed, you can do that yourself.

Jeff "japhy" Pinyan -- accomplished author, consultant, hacker, and teacher


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives