Step 4: Use perl's CGI library and a form with Post action to display an input box and get the result
Perl has a standard CGI library that has a command param that will grab session variables.
Fact to know: The browser does not recognize $word = <STDIN>, so we have to find another way to get user input via the internet. This is one way.
Fact to know: To use the CGI library, you need to include the following before using the param command:
use strict;
use warnings;
use CGI qw/:standard/;
Fact to know: The param function is given the name of the text field on the form and returns the value typed into the text field.
Fact to know: When your form action is set to the same form, the entire script will be completely reprocessed with the new input from the form fields.
Fact to know: We are using the cgi.pm library as a set of functions, and not creating an object of it. This is simpler but many tutorials use it in an object oriented manner. We are only taking advantage of the param function, but many more would be helpful.
Learning to do first
Please read this: https://www.cs.tut.fi/~jkorpela/perl/cgi.html but you can stop after "just echo the data section is read" (about 1/3 of the way down).
You may prefer this: http://docstore.mik.ua/orelly/perl/learn32/ch18_04.htm but stop at "other form elements"
You might like this tutorial, but it quickly gets more complex than we need: http://www.perl.com/pub/2000/12/begperl4.html
Your job:
Create a perl script that displays an html file with a form input text field that has a post action calling itself. In your script, capture the value of the input text field into a variable and display it on the screen.
Instructions for doing this:
Open step 3 or any perl script that displays an html file with an input form text field.
Change the form method to post and the form action to the same name as the script, so it will call itself upon submit. For example (though you want to call your script, and have a search field prompt instead):
<form method="POST" action="action_page.pl">
First name:<br>
<input type="text" name="firstname" value="Mickey">
<br>
Last name:<br>
<input type="text" name="lastname" value="Mouse">
<br><br>
<input type="submit" value="Submit">
</form>Add the CGI library and convert to strict with warnings by placing these lines after you print the HTML plain text line:
use strict;
use warnings;
use CGI qw/:standard/;Add a line that just under that CGI library definition that takes in the parameter or sets it empty if it cannot take in the parameter. In the example below, the param is firstname, but yours should be named input:
my $search = param("firstname")||"";
if ($search)
{print qq(<br /> the search word is $search <br />);}
else
{print qq(<br /> there is no search word entered <br/>):}
Test this in panther using perl yourscriptname. It should show the html code and will not have a search word filled.
Test this in your browser and see the intput field and the words that there is no search word entered. Press submit and see the contents of your field.
You now know how to grab the contents of an input text box so you can use it in your program.