One of mod_perl's benefits is that it can run existing CGI scripts written in Perl that were previously used under mod_cgi (the standard Apache CGI handler). Indeed, mod_perl can be used for running CGI scripts without taking advantage of any of mod_perl's special features, while getting the benefit of the potentially huge performance boost. Example 2-1 gives an example of a very simple CGI-style mod_perl script.
print "Content-type: text/plain\n\n"; print "mod_perl rules!\n";
Save this script in the /home/stas/modperl/mod_perl_rules1.pl file. Notice that the #! line (colloquially known as the shebang line) is not needed with mod_perl, although having one causes no problems, as can be seen in Example 2-2.
#!/usr/bin/perl print "Content-type: text/plain\n\n"; print "mod_perl rules!\n";
Now make the script executable and readable by the server, as explained in the previous section:
panic% chmod 0700 /home/stas/modperl/mod_perl_rules1.pl
The mod_perl_rules1.pl script can be tested from the command line, since it is essentially a regular Perl script:
panic% perl /home/stas/modperl/mod_perl_rules1.pl
This should produce the following output:
Content-type: text/plain mod_perl rules!
Make sure the server is running and issue these requests using a browser:
If the port being used is not 80 (e.g., 8080), the port number should be included in the URL:
Also, the localhost approach will work only if the browser is running on the same machine as the server. If not, use the real server name for this test. For example:
The page rendered should be similar to the one in Figure 2-1.
If you see it, congratulations! You have a working mod_perl server.
If something went wrong, go through the installation process again, making sure that none of the steps are missed and that each is completed successfully. You might also look at the error_log file for error messages. If this does not solve the problem, Chapter 3 will attempt to salvage the situation.
Jumping a little bit ahead, Example 2-3 shows the same CGI script written with the mod_perl API.
my $r = Apache->request; $r->send_http_header('text/plain'); $r->print("mod_perl rules!\n");
The mod_perl API needs a request object, $r, to communicate with Apache. The script retrieves this object and uses it to send the HTTP header and print the irrefutable fact about mod_perl's coolness.
This script generates the same output as the previous one.
As you can see, it's not much harder to write your code using the mod_perl API. You need to learn the API, but the concepts are the same. As we will show in the following chapters, usually you will want to use the mod_perl API for better performance or when you need functionality that CGI doesn't provide.
mod_perl, modperl, Apache, perl, cgi, html, mod_perl, e-commerce, scalability, free, open source, OSS, apache, squid, high availability, modperl, linux, unix, Web, www, mod_perl, webserver, admin, apache, book, webmaster, tools, modperl, guide, docs, documentation, help, mod_perl, perl, information, apache, script, errata, eric cholet, perl, apache, mod-perl, stas bekman, mod_perl, cool, perl, Apache, performance, speed, choice
Other projects to check out: meta-religion.com is for those interested in Religious, Spiritual and Esoteric Phenomena. i-want-a-better.com is a community of people discussing what they would like to be improved in their lives and things they use and interact with. You may also want to find a healer in your area or read articles on variety of topics.