Summary

Not everything to do with Perl has to involve long and involved scripts with lots of subroutines. Sometimes the most useful things in Perl can be done using modules with a little glue code to get the result you want. CGI is a terrific example of this; the CGI.pm module covers up most of the gritty parts of CGI scripting and makes it easy to get the values from a form or other CGI submission and return an HTML file as the result.

Today you learned something about using Perl for CGI, including how CGI works from the browser to the server to the script and back again; how CGI.pm can be imported and used in your script, and how to use its various features to make your life with CGI more pleasant. We also explored a couple of CGI examples: a survey form that keeps track of the survey data in an external file, and a color-generation script that is its own form and which maintains the current form values each time the form is regenerated. I can't guarantee that this lesson has given you everything you need to create CGI scripts (because it hasn't), but hopefully it's given you a starting point from which you can work.

The subroutines you've learned about in this lesson are all part of the CGI.pm module, and include

  • param() gets the parameters that were given to the CGI script, usually as part of a form submission. param() with no arguments returns a list of the keys (form element names) available; param() with a key argument returns the value for that key.

  • print header() prints the CGI header for the output. The print_header() subroutine without any arguments assumes the output will be in HTML format.

  • start_html() returns the top part of an HTML page, including the <html>, <head>, <title> and <body> tags. Different arguments to the start_html() tag will generate different values for the HTML (for example, a single string argument will set the title of the page.

  • end_html() generates closing </body> and </html> tags for the output.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset