[plug] Perl CGI scripts

Paul Wilson hooker at opera.iinet.net.au
Wed Nov 4 07:53:18 WST 1998


> From: P Robinson <ribbo at iinet.net.au>
> 
> I'm just venturing into Perl CGI scripts, well CGI scripts in general,
and
> am having a few problems.
> 
> To summarize my problems, internet.au had a quick article about CGI in
> their november issue, I tried to replicate thier initial form to get it
> working so I could mess around with it too produce something I could use
> and expand for my own requirements.
> 
> Anyway they didn't really cover everything, so I made the assumption
> that the script should be world executable and reside in the cgi-bin dir
> but it still doesn't work.
> 
> Can someone give me some general pointers to help me start out, and
> possibly an example. Once I get going I'm usually fine, its just the mag
> is stupid and just gives you the two sample files and a general intro to
> CGI which I already knew.
> 
> Thanks
> Peter

Peter, try this as a short example of how CGI passes parameters (if this
isn't your problem, then I apologise) - call it example and it lives in the
cgi-bin directory :

#!/usr/bin/perl
#======================================================================

&cgi_params;

print "<html><head><title>Test</title></head><body>\n";

foreach $k (sort keys %FORM)
{
	print "Key = $k, value = ", $FORM {$k}, "<br>\n";
}
print "<hr></body></html>\n";

exit 0;

#==================================================================
#------------------------------>CGI_PARAMS
# Extract the parameters for a form
#  Return them in %FORM
#
sub cgi_params
{
	my ($incoming, $name, $value);

	if ($ENV {'REQUEST_METHOD'} eq "POST")
	{
		read (STDIN, $incoming, $ENV {'CONTENT_LENGTH'});
	}
	else
	{
		$incoming = $ENV {'QUERY_STRING'};
	}

	foreach (split (/&/, $incoming))
	{
		($name, $value) = split(/=/, $_);

		$name  =~ tr/+/ /;
		$value =~ tr/+/ /;
		$name  =~ s/%([A-F0-9][A-F0-9])/pack("C", hex($1))/gie;
		$value =~ s/%([A-F0-9][A-F0-9])/pack("C", hex($1))/gie;

# Strip out semicolons unless for special character
		$value =~ s/;/$$/g;
		$value =~ s/&(\S{1,6})$$/&\1;/g;
		$value =~ s/$$/ /g;

		$value =~ s/\|/ /g;
		$value =~ s/^!/ /g; ## Allow exclamation points in sentences

# Check for "assign-dynamic" field names
#  Mainly for on-the-fly input names, especially checkboxes
		if ($name =~ /^assign-dynamic/)
		{
			$name = $value;
			$value = "on";
		}

# Allow for multiple values of a single name
		$FORM {$name} .= "," if ($FORM {$name});

		$FORM {$name} .= $value;
	}
}


Then create a web page which includes :

<form method=post action="/cgi-bin/example">
<input type=text name=surname>
<input type=radio name=radio value="01">Frist choice<br>
<input type=radio name=radio value="02">Second choice<br>
</form>

You should get back a web page generated by the Perl script above showing
two variables "surname" and "radio" and their contents.

If this doesn's help/work or you need to ask more question, then please do
(email me as <a
href="mailto:hooker at opera.iinet.net.au">hooker at iinet.net.au</a> if you'd
prefer to bye-pass the list.

Regards,

Paul


More information about the plug mailing list