|
||||||
How to Use a Form in a Perl CGI Web PageCreating a Simple Perl User Interface with the CGI ModuleNothing could be simpler than adding a form to a Perl CGI web page, and if a programmer uses the Perl CGI module then they can easily process the information it contains
Once a Perl programmer has access to a web server (such as Apache) then they'll have a Perl based web page up and running in no time at all (as discussed in How to Use Perl Programming on a Web Site). They will be able to create a web page that runs Perl code on the server and which returns a HTML output to the users' browsers. However, that's a one way process - from the server to the user. The next step is, of course, to make this a two way process and enable the user to send information to the server as well as receiving information from it. The obvious answer is for the perl Programmer to:
Or, the programmer can use the Perl CGI module. Using the CGI ModuleIt is quite feasible for the Perl programmer to use the inputs to a web page by processing the information sent by the POST and GET methods. However, there is no need for them to do that. Instead they just need to make use of one of the modules that comes with their Perl installation. That module is the Perl CGI module and is loaded by adding the following code to a script: use CGI qw/:standard/;
They will now be able to use the param method which can access any GET or POST variable by name. For example: #!c:/strawberry/perl/bin/perl
print "Content-type: text/html\n\n";
print "<h1>" . param ('greeting') . "</h2>";
Here's the script:
If this file is saved into the server's CGI directory ad "helloworld.pl" then it can be displayed in a browser by using the URL: http://localhost/cgi-bin/helloworld.pl?greeting=hello
This will display the required text as shown in Figure 1 at the bottom of this article. Using Forms with Perl CGI, GET and POSTThe technique shown above uses the GET method to send information to the Perl script. However, that is inherently insecure since the variable is passed as part of the web page's URL. It is, therefore, much safer to use a form and the POST method. This is another task made very simple by the Perl CGI module: if (param ('greeting')) {
print "<h1>" . param('greeting') . "</h2>";
} else {
print start_form,
"Enter a greeting:", textfield ('greeting'), p,
submit ('Send Greeting'),
end_form;
}
The copyright of the article How to Use a Form in a Perl CGI Web Page in Computer Programming Tutorials is owned by Mark Alexander Bain. Permission to republish How to Use a Form in a Perl CGI Web Page in print or online must be granted by the author in writing.
|
||||||
|
|
||||||
|
|
||||||