MOD

zhaozj2021-02-16  52

MOD_PERL programming introduction

1. Introduction Guide MOD_PERL is a huge and complex tool, which has built many modules to help you build a dynamic site. The purpose of this guide is to help you build a good MOD_PERL module and understand the implementation techniques of MOD_PERL. I don't recommend using the techniques introduced here to build a large site, especially for people who have just been involved in mod_perl. But I recommend you to go deep into some of its built-in programs, such as Mason, Axkit, Embperl, Apache :: ASP, and Pagekit, and more. 1.1. What do you need? This guide assumes that you have experienced experience in MOD_PERL. And the installation experience of the new version of Apache. Because it is possible that you need to modify the configuration provided herein when you implement it on your machine. We need you to install some modules and need to enter the Apache configuration directory for modification. So the best of you have root privileges to do these things. Of course, you still need a text editor. 1.2. Cutting the topic MOD_PERL module is also a Perl module, but it has a more special design. The most convenient way to create a Perl module is to use standard Perl distribution

H2xs

. You can type H2xs in the command line mode to see its parameter list. Now, get a new project to a proper directory, type:

H2XS -A -X -N Apache :: Tutorial :: First

H2XS will create a directory Apache, as well as some other subdirectory. Now enter the most deepest directory to see:

CD Apache / Tutorial / First

In this new directory, you can see 5 files: Changes, First.PM, Manifest, Makefile.pl and Test.pl. Their roles are as follows:

Changes This file is a modification log for your project first.pm This is the main module file, which contains your MOD_PERL handle code. The Manifest This document is used to automatically build a TAR.GZ type module version distribution. This way you can get your module to the CPAN release or distribute it to others. It contains a list of all files in this project. Makefile.pl This is a standard Perl Makefile constructor. Used to create a makefile.pl file to compile this module. Test.pl For some test scripts of the module. By default it is just checking the compilation and load of the module, now we start to turn first.pm to workable mod_perl modules. Use the text editor to open the file, the modified content is as follows:

Package Apache :: Tutorial :: first;

Use strict;

Use Vars QW / $ VERSION /;

Use apache :: constants;

$ Version = 0.01;

Sub handler {

MY $ r = shift;

$ r-> send_http_header ('text / html');

Print " Hello World ";

Return OK;

}

1;

Don't forget "1;", for Perl, a non-zero value returned by a module indicates that the module has been successfully compiled. 1.3. Installing your module H2XS tool makes our module installation work extremely convenient. In the same directory as your first.pm file. , Type:

Perl makefile.pl

Make

Make test

If Make Test is successful, you need to perform as root:

Make Install

This way you install your module to the library directory. 1.4. Add this module to a handle of Apache (Handler) now we need to enter the Apache configuration directory to modify the configuration file, so that our module is a processor in the Apache content processing phase. Open httpd.conf file, add the following configuration at the end:

SetHandler Perl-script

Perlhandler Apache :: Tutorial :: First

Then save the configuration file and restart the APACHE server:

Apachectl Stop

Apachectl Start

Now use the browser to access http: // localhost / mod_perl_tutorial, you will see the "Hello World" page as scheduled. 2. What happened here? Good, what happened here? When the Apache started, it read its configuration instruction and passed the appropriate command to the corresponding module that handles the command. There are two related instructions setHandler and Perlhandler. The first instruction setHandler handles the MOD_MIME module, which means what module is used as the main part of the processing request. The Perl-script set here represents using mod_perserl to process the request. The second instruction PerlHandler is processed by the Mod_perl module, which is just a simple description using our modules to process the main part of the request. One thing to note, whenever you have a Perlhandler, you need the corresponding setHandler Perl-Script configuration instruction. This will make your MOD_PERL code work. I always think this is a weakness, but this will involve the internal processing mechanism within Apache, so it is difficult to change in the future. Now request, Apache View what module to handle the corresponding URI and decide to use MOD_PERL, and mod_perl knows that it must send the request to our module, and call our module's handler () function as apache :: request object The first parameter. And our return value of our handler () function determines what the next apache will do. Now that we know that the return value is OK means that everything is successful. OK is a constant exported from the Apache :: Constants module. 3. Debug if you didn't see "Hello World", you might see an error page, or anything else is completely different. The first step will check the error log to see what is wrong. I am used to view the error log immediately after requesting the browser. You can use the TAIL tool:

Tail -f / path / to / apache / logs / error_log

(Use your real error_log path to replace the path above. If you don't sure it, check the ERRORLOG instruction section of your httpd.conf file) Now reload the page, then Error_Log will tell you where there is a problem. For more information on Perl, see

PERDEBUG. 4. Join more now if you want some modifications to the above situation, what should I do? Unfortunately, the only installation mode is as follows:

Modify your first.pm file Run Make Install as root to restart Apache This may be very troublesome, especially restarting apache. In response to this problem, we can additionally install a specially designed module to avoid trouble every time. First you need to download and install the Apache :: Reload module from the CPAN (unless you have used mod_perl 1.26 or higher). Here http://search.cpan.org/search?dist=apache-reload download. Unnock the tar.gz file and enter the new directory, execute: perl makefile.pl

Make

Then go to the root as follows:

Make Install

Now open httpd.conf files again, join:

PerlinitHandler Apache :: Reload

This will test all changed modules and automatically reload new modules if necessary. This is useful for development, but there will be performance losses, so after the development is complete, it will close this property. 5. Read more from here, you have a lot of things to do. Apache API itself is very large, most can see the corresponding documentation through Perldoc Apache. Now this module is basically worthless, because only one URI can be used by the module (http: // server / mod_perl_tutorial) This makes it not flexible enough. In order to make a module can handle multiple URIs, there are many solutions, but the best is recommended to use the Apache :: Dispatch module. You can download http://search.cpan.org/search?cpache-dispatch. Apache :: Dispatch allows you to retain the standard Mod_Perl Handler architecture while allowing multiple functions and multiple URIs to be distributed. Next I don't recommend the display of the browser directly as the examples. Consider using some common template technology, such as Template-Toolkit, HTML :: Template, more more than using XSLT or XPathScript (there are many such template techniques, we hope that one day can have articles to discuss these technologies to help You choose).

转载请注明原文地址:https://www.9cbs.com/read-27982.html

New Post(0)