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.

You have not mentioned what are the problems you have for this example.

By the way , what webserver are you using ?

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:

#!/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.

... and cbkihong should know, he wrote an extensive, 241 page book on PERL!

cbkihong's PERL book

Great job!

Kudos cbkihong! I just downloaded a copy of the pdf.

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!