The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Pulling Input from HTML into Shell mosammey Shell Programming and Scripting 1 02-17-2008 07:02 PM
Using Perl to add hyperlink to html files Raynon Shell Programming and Scripting 17 11-01-2007 02:03 AM
HTML parsing by PERL avik1983 Shell Programming and Scripting 3 02-23-2007 06:25 AM
perl and html marcpascual Shell Programming and Scripting 8 09-21-2005 04:47 PM
Passing FORM(HTML) variable to ksh douknownam Shell Programming and Scripting 1 02-25-2005 01:50 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 02-11-2005
Registered User
 

Join Date: May 2004
Posts: 35
Perl: Variable input via HTML

I am completely new to perl and am just going over the tutorials right now. What I am trying to attempt is to take the input from the HTML (in a form) and use those variables in a perl script. I've looked everywhere for a simple example on how to do this and cannot find it or do not understand what they mean. Here is what I have below:

#!/bin/perl
print "Content-type: text/html\n\n";

print <<ENDHTML;
<HTML>
<HEAD>
<TITLE>CGI Test</TITLE>
</HEAD>
<BODY>
<FORM METHOD=POST ACTION="../cgi-bin/mycgi.pl">
name: <INPUT TYPE=TEXT NAME="realname"><BR>
email: <INPUT TYPE=TEXT NAME="email"><BR>
<INPUT TYPE=SUBMIT>
</FORM>
</BODY>
</HTML>

ENDHTML
print "My name is: $realname\n";
print "My email is: $email\n";

Thanks in advance for any suggestions.
Reply With Quote
Forum Sponsor
  #2  
Old 02-11-2005
bhargav's Avatar
Registered User
 

Join Date: Sep 2004
Location: USA
Posts: 511
You have not mentioned what are the problems you have for this example.

By the way , what webserver are you using ?

Last edited by bhargav; 02-13-2005 at 12:05 AM.
Reply With Quote
  #3  
Old 02-12-2005
Moderator
 

Join Date: Sep 2002
Location: Hong Kong, China
Posts: 1,469
First, make sure you have a proper shebang for perl. /bin/perl? I rarely see perl installed in /bin, but if it's indeed there then forget it.

Modern perl scripters no longer craft hacks to read form data from standard input primitively. In those code, POST form data used to be read from <STDIN> according to CGI specification, from which the content in the form of &-separated key=value pairs are usually extracted with a combination of split() and regular expressions. You may still find examples with code like this. However, this is prone to errors and is no longer recommended. The Perl community has agreed on a common standard - the CGI module. If your tutorial doesn't mention the CGI module, it should be considered out of date already and you ought to cast some questioning glances on it.

Here I modified your script a little bit to put in the missing parts. The CGI module knows how to find out whether a POST or GET form is involved, and then read and extract form data with the appropriate method for you. No fiddling of those dirty things required anymore:

Code:
#!/usr/bin/perl

use CGI;

my $cgi = new CGI;
print $cgi->header('text/html');

if (defined $cgi->param('ok')) {
 my %params = $cgi->Vars;
 print "My name is: ${params{realname}}<br>";
 print "My email is: ${params{email}}<br>";
} else {
print <<ENDHTML;
<HTML>
<HEAD>
<TITLE>CGI Test</TITLE>
</HEAD>
<BODY>
<FORM METHOD=POST ACTION="mycgi.pl">
name: <INPUT TYPE=TEXT NAME="realname"><BR>
email: <INPUT TYPE=TEXT NAME="email"><BR>
<INPUT TYPE=SUBMIT NAME="ok">
</FORM>
</BODY>
</HTML>

ENDHTML
}
Some perl people actually recommend you to use the CGI object exclusively to output the form completely with its API rather than hardcoding the HTML tags in print() statements, but I think this is just a matter of taste. Isolating form snippet in templates and construct form output dynamically with a template engine (such as Template Toolkit) is seen as an even better approach.

Just put the script (mycgi.pl) in your Web server, give it executable chmod, and that should work. It should work if you have a reasonably recent version of CGI. It should be, if your Perl version is recent enough. The CGI module is shipped with Perl by default.

Last edited by cbkihong; 02-12-2005 at 08:00 PM. Reason: use $cgi->header() instead of legacy way
Reply With Quote
  #4  
Old 02-13-2005
Neo's Avatar
Neo Neo is offline
Administrator
 

Join Date: Sep 2000
Location: Asia Pacific
Posts: 4,441
... and cbkihong should know, he wrote an extensive, 241 page book on PERL!

cbkihong's PERL book

Great job!
Reply With Quote
  #5  
Old 02-13-2005
Registered User
 

Join Date: Oct 2002
Posts: 676
Kudos cbkihong! I just downloaded a copy of the pdf.
Reply With Quote
  #6  
Old 02-13-2005
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,616
Great Job! I've often been impressed by cbkihong's perl expertise. But I want to point out that the book is available on a mirror site. cbkihong's web site is being hammered by downloads, so please consider using the mirror. Also note that cbkihong is looking for other mirror sites for his book. Let him know if you can help. Thanks!
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
regex, regular expressions

Thread Tools
Display Modes




All times are GMT -7. The time now is 03:04 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0