|
||||
|
|
Your First Perl Web Application
Perl for the WebPerl has a long relationship with the web. Although it wasn't designed exclusively for web solutions, millions of web sites have used Perl via CGI scripts and mod_perl to offer dynamic content to their users. With its relative ease of use, inclusion in almost every UNIX/Linux hosting package available, and wealth of pre-existing programs and application frameworks, Perl remains a solid choice for web development. High-profile sites like Amazon.com, Google, and Slashdot.org use Perl applications to drive hundreds of millions of page views per day, the language has certainly proven its scalability when used properly. You're probably not planning on using Perl to drive a million-dollar website (at least not for a while), and that's okay; we're going to start really small with a couple of basic examples of CGI scripts. To use these examples, you'll need to have a web server package like Apache or Microsoft IIS installed on your web server or desktop.
"Hello World" for the Web in Perl#!/usr/bin/perl -w use strict; my $message = "Hello, world! We're using Perl."; print "Content-type:text/html\n\n"; print <<HTMLSTOP <html> <head> <title>My First Web-Based Perl Script</title> </head> <body> <h1>$message</h1> HTML output in Perl is really easy. </body> </html> HTMLSTOP Depending on your web server's configuration, you may need to save your scripts to a folder like "cgi-bin" under the root of your website. Also, you may need to save your Perl scripts with a ".cgi" extension, and you need to make sure that file permissions are properly set (generally speaking, you should allow all users to read and execute the scripts). Let's take a look at how our example works. When you run Perl in concert with web server software, the server (typically Apache) handles the job of making a network connection between the web site user and your script. By default, standard output is redirected to the visitor's browser instead of appearing in a console window or log file. If your goal is to send HTML to the visitor's browser (99% of time this is the case), you need to include code that sends a proper header to the browser. This is what the first print statement in our example does. There are ways to send a more complete and standards-compliant browser header (such as the methods used in CGI.pm), but our technique is sufficient for learning purposes. Once the header string is sent, you can use additional print statements in any fashion you like to write HTML to the browser. The next chapter will show you how to a simple web application that accepts user input and acts on the supplied values. Continue: Getting User Input with HTML Forms Table of Contents
|
|||
| All contents Copyright © 2007-2011 ClassHelper.org®. Please see our Privacy Policy. | ||||