Module Versioning with Apache 1.x and mod_perl 1.x
1. Module
Versioning
Theron
Stanford
The Challenge Module Versioning
The Idea
The Keys to
with Apache 1.x and mod_perl 1.x
the Solution
Declaring Packages
Loading Modules
Configuring Mason
Modifying httpd.conf
Theron Stanford
Demonstration
Time! Oversee.net
April 17, 2008
2. Software Framework
Module
Versioning Apache 1.3
Theron
Stanford Perl 5.8
The Challenge
mod_perl 1.29
The Idea HTML::Mason 1.32
The Keys to
the Solution
Declaring Packages
Loading Modules
Configuring Mason
Modifying httpd.conf
Demonstration
Time!
3. Software Framework
Module
Versioning Apache 1.3
Theron
Stanford Perl 5.8
The Challenge
mod_perl 1.29
The Idea HTML::Mason 1.32
The Keys to
the Solution
Declaring Packages
Loading Modules
Configuring Mason
Modifying httpd.conf
Demonstration
Time!
4. Software Framework
Module
Versioning Apache 1.3
Theron
Stanford Perl 5.8
The Challenge
mod_perl 1.29
The Idea HTML::Mason 1.32
The Keys to
the Solution
Declaring Packages
Loading Modules
Configuring Mason
Modifying httpd.conf
Demonstration
Time!
5. Software Framework
Module
Versioning Apache 1.3
Theron
Stanford Perl 5.8
The Challenge
mod_perl 1.29
The Idea HTML::Mason 1.32
The Keys to
the Solution
Declaring Packages
Loading Modules
Configuring Mason
Modifying httpd.conf
Demonstration
Time!
6. Original Directory Tree
Module
Versioning
Theron /usr/local/perlmongers
Stanford
The Challenge lib www
The Idea
perl5 mason
The Keys to
the Solution
Declaring Packages PerlMongers
Loading Modules
Configuring Mason
Modifying httpd.conf
Demonstration
The standard setup is like this. Apache looks for the
Time! Mason files in www (www/mason is used for the cache) and
the modules in lib/perl5/PerlMongers.
7. Desired Directory Tree
Module
Versioning
Theron /usr/local/perlmongers
Stanford
The Challenge
path1 path2
The Idea
lib www lib www
The Keys to
the Solution
Declaring Packages perl5 mason perl5 mason
Loading Modules
Configuring Mason
Modifying httpd.conf
PerlMongers PerlMongers
Demonstration
Time!
We want Apache and mod_perl to be able to load both
branches and serve the content depending on the URL.
We’d like to do this without having to change the names of
the files or making any code changes within the modules.
8. Desired Directory Tree
Module
Versioning
Theron /usr/local/perlmongers
Stanford
The Challenge
path1 path2
The Idea
lib www lib www
The Keys to
the Solution
Declaring Packages perl5 mason perl5 mason
Loading Modules
Configuring Mason
Modifying httpd.conf
PerlMongers PerlMongers
Demonstration
Time!
We want Apache and mod_perl to be able to load both
branches and serve the content depending on the URL.
We’d like to do this without having to change the names of
the files or making any code changes within the modules.
9. The Difficulty
Module
Versioning With Apache 1.x and mod_perl 1.x, there is only one Perl
Theron
Stanford
interpreter that all the code goes into.
This means that we can’t load two modules that declare
The Challenge
the same package without one clobbering the other.
The Idea
The Keys to It also means we can’t (easily) use two modules with the
the Solution
Declaring Packages
same name, even if they’re in different directories — Perl’s
Loading Modules
Configuring Mason
internal mechanism for managing modules is set up to
Modifying httpd.conf
prevent duplicate loading.
Demonstration
Time!
10. The Difficulty
Module
Versioning With Apache 1.x and mod_perl 1.x, there is only one Perl
Theron
Stanford
interpreter that all the code goes into.
This means that we can’t load two modules that declare
The Challenge
the same package without one clobbering the other.
The Idea
The Keys to It also means we can’t (easily) use two modules with the
the Solution
Declaring Packages
same name, even if they’re in different directories — Perl’s
Loading Modules
Configuring Mason
internal mechanism for managing modules is set up to
Modifying httpd.conf
prevent duplicate loading.
Demonstration
Time!
11. The Difficulty
Module
Versioning With Apache 1.x and mod_perl 1.x, there is only one Perl
Theron
Stanford
interpreter that all the code goes into.
This means that we can’t load two modules that declare
The Challenge
the same package without one clobbering the other.
The Idea
The Keys to It also means we can’t (easily) use two modules with the
the Solution
Declaring Packages
same name, even if they’re in different directories — Perl’s
Loading Modules
Configuring Mason
internal mechanism for managing modules is set up to
Modifying httpd.conf
prevent duplicate loading.
Demonstration
Time!
12. The Idea
Module
Versioning Modify the module code on the fly at load time so that
Theron
Stanford
modules from different branches end up in non-conflicting
packages.
The Challenge
(think of C++ function name mangling — but not so
The Idea
ugly!)
The Keys to
the Solution Set up the Apache configuration and Perl handlers to use
Declaring Packages
Loading Modules the correct module versions and Mason files.
Configuring Mason
Modifying httpd.conf
Demonstration
Time!
13. The Idea
Module
Versioning Modify the module code on the fly at load time so that
Theron
Stanford
modules from different branches end up in non-conflicting
packages.
The Challenge
(think of C++ function name mangling — but not so
The Idea
ugly!)
The Keys to
the Solution Set up the Apache configuration and Perl handlers to use
Declaring Packages
Loading Modules the correct module versions and Mason files.
Configuring Mason
Modifying httpd.conf
Demonstration
Time!
14. The Keys to the Solution
Module
Versioning Declaring Packages
Theron
Stanford Loading Modules
The Challenge
Configuring Mason
The Idea Modifying httpd.conf
The Keys to
the Solution
Declaring Packages
Loading Modules
Configuring Mason
Modifying httpd.conf
Demonstration
Time!
15. Packages and Namespaces
Module
Versioning Every variable belongs to a package whose name is a
Theron
Stanford
namespace.
In simple scripts with no package declarations, every
The Challenge
variable belongs to package main. The symbols $var,
The Idea
The Keys to
$::var, and $main::var all refer to the same variable.
the Solution
Declaring Packages
One changes packages with a
Loading Modules
Configuring Mason package NAMESPACE
Modifying httpd.conf
Demonstration
statement.
Time!
A typical use of this is in modules. For instance, the
module PerlMongers/Greeting.pm would begin with the
statement package PerlMongers::Greeting.
16. Packages and Namespaces
Module
Versioning Every variable belongs to a package whose name is a
Theron
Stanford
namespace.
In simple scripts with no package declarations, every
The Challenge
variable belongs to package main. The symbols $var,
The Idea
The Keys to
$::var, and $main::var all refer to the same variable.
the Solution
Declaring Packages
One changes packages with a
Loading Modules
Configuring Mason package NAMESPACE
Modifying httpd.conf
Demonstration
statement.
Time!
A typical use of this is in modules. For instance, the
module PerlMongers/Greeting.pm would begin with the
statement package PerlMongers::Greeting.
17. Packages and Namespaces
Module
Versioning Every variable belongs to a package whose name is a
Theron
Stanford
namespace.
In simple scripts with no package declarations, every
The Challenge
variable belongs to package main. The symbols $var,
The Idea
The Keys to
$::var, and $main::var all refer to the same variable.
the Solution
Declaring Packages
One changes packages with a
Loading Modules
Configuring Mason package NAMESPACE
Modifying httpd.conf
Demonstration
statement.
Time!
A typical use of this is in modules. For instance, the
module PerlMongers/Greeting.pm would begin with the
statement package PerlMongers::Greeting.
18. Packages and Namespaces
Module
Versioning Every variable belongs to a package whose name is a
Theron
Stanford
namespace.
In simple scripts with no package declarations, every
The Challenge
variable belongs to package main. The symbols $var,
The Idea
The Keys to
$::var, and $main::var all refer to the same variable.
the Solution
Declaring Packages
One changes packages with a
Loading Modules
Configuring Mason package NAMESPACE
Modifying httpd.conf
Demonstration
statement.
Time!
A typical use of this is in modules. For instance, the
module PerlMongers/Greeting.pm would begin with the
statement package PerlMongers::Greeting.
19. How Packages Help
Module
Versioning The plan is to modify the module namespaces according
Theron
Stanford
to which subdirectory they come from.
If a module is in perlmongers/pathn/lib/perl5, then
The Challenge
its package is changed from PerlMongers::* to
The Idea
The Keys to
pathnPerlMongers::*, where n is 1 or 2.
the Solution
Declaring Packages
All references to PerlMongers::* inside any module
Loading Modules
Configuring Mason
must also be changed to pathnPerlMongers::*.
Modifying httpd.conf
If a module uses another PerlMongers::* module, then
Demonstration
Time! this second module must also have all of its references to
PerlMongers::* changed to pathnPerlMongers::*.
We must take into account circular uses of use.
We must make sure we don’t accidentally do something
like changing PerlMongers::* to
path1path1PerlMongers::*.
20. How Packages Help
Module
Versioning The plan is to modify the module namespaces according
Theron
Stanford
to which subdirectory they come from.
If a module is in perlmongers/pathn/lib/perl5, then
The Challenge
its package is changed from PerlMongers::* to
The Idea
The Keys to
pathnPerlMongers::*, where n is 1 or 2.
the Solution
Declaring Packages
All references to PerlMongers::* inside any module
Loading Modules
Configuring Mason
must also be changed to pathnPerlMongers::*.
Modifying httpd.conf
If a module uses another PerlMongers::* module, then
Demonstration
Time! this second module must also have all of its references to
PerlMongers::* changed to pathnPerlMongers::*.
We must take into account circular uses of use.
We must make sure we don’t accidentally do something
like changing PerlMongers::* to
path1path1PerlMongers::*.
21. How Packages Help
Module
Versioning The plan is to modify the module namespaces according
Theron
Stanford
to which subdirectory they come from.
If a module is in perlmongers/pathn/lib/perl5, then
The Challenge
its package is changed from PerlMongers::* to
The Idea
The Keys to
pathnPerlMongers::*, where n is 1 or 2.
the Solution
Declaring Packages
All references to PerlMongers::* inside any module
Loading Modules
Configuring Mason
must also be changed to pathnPerlMongers::*.
Modifying httpd.conf
If a module uses another PerlMongers::* module, then
Demonstration
Time! this second module must also have all of its references to
PerlMongers::* changed to pathnPerlMongers::*.
We must take into account circular uses of use.
We must make sure we don’t accidentally do something
like changing PerlMongers::* to
path1path1PerlMongers::*.
22. How Packages Help
Module
Versioning The plan is to modify the module namespaces according
Theron
Stanford
to which subdirectory they come from.
If a module is in perlmongers/pathn/lib/perl5, then
The Challenge
its package is changed from PerlMongers::* to
The Idea
The Keys to
pathnPerlMongers::*, where n is 1 or 2.
the Solution
Declaring Packages
All references to PerlMongers::* inside any module
Loading Modules
Configuring Mason
must also be changed to pathnPerlMongers::*.
Modifying httpd.conf
If a module uses another PerlMongers::* module, then
Demonstration
Time! this second module must also have all of its references to
PerlMongers::* changed to pathnPerlMongers::*.
We must take into account circular uses of use.
We must make sure we don’t accidentally do something
like changing PerlMongers::* to
path1path1PerlMongers::*.
23. How Packages Help
Module
Versioning The plan is to modify the module namespaces according
Theron
Stanford
to which subdirectory they come from.
If a module is in perlmongers/pathn/lib/perl5, then
The Challenge
its package is changed from PerlMongers::* to
The Idea
The Keys to
pathnPerlMongers::*, where n is 1 or 2.
the Solution
Declaring Packages
All references to PerlMongers::* inside any module
Loading Modules
Configuring Mason
must also be changed to pathnPerlMongers::*.
Modifying httpd.conf
If a module uses another PerlMongers::* module, then
Demonstration
Time! this second module must also have all of its references to
PerlMongers::* changed to pathnPerlMongers::*.
We must take into account circular uses of use.
We must make sure we don’t accidentally do something
like changing PerlMongers::* to
path1path1PerlMongers::*.
24. How Packages Help
Module
Versioning The plan is to modify the module namespaces according
Theron
Stanford
to which subdirectory they come from.
If a module is in perlmongers/pathn/lib/perl5, then
The Challenge
its package is changed from PerlMongers::* to
The Idea
The Keys to
pathnPerlMongers::*, where n is 1 or 2.
the Solution
Declaring Packages
All references to PerlMongers::* inside any module
Loading Modules
Configuring Mason
must also be changed to pathnPerlMongers::*.
Modifying httpd.conf
If a module uses another PerlMongers::* module, then
Demonstration
Time! this second module must also have all of its references to
PerlMongers::* changed to pathnPerlMongers::*.
We must take into account circular uses of use.
We must make sure we don’t accidentally do something
like changing PerlMongers::* to
path1path1PerlMongers::*.
25. Loading Modules
Module
Versioning Perl uses two arrays, @INC and %INC, to keep track of
Theron
Stanford
module loading.
@INC is a list of directories Perl searches in order to find
The Challenge
the module.
The Idea
The Keys to %INC is a hash that stores the complete path of the loaded
the Solution
Declaring Packages
module. This prevents reloading.
Loading Modules
Configuring Mason
Because these variables are in the global namespace, users
Modifying httpd.conf
can modify them at will:
Demonstration
Time! Example: adding a directory to the front of @INC (this is
essentially how use lib works)
Example: deleting an entry in %INC to force reloading of a
module
26. Loading Modules
Module
Versioning Perl uses two arrays, @INC and %INC, to keep track of
Theron
Stanford
module loading.
@INC is a list of directories Perl searches in order to find
The Challenge
the module.
The Idea
The Keys to %INC is a hash that stores the complete path of the loaded
the Solution
Declaring Packages
module. This prevents reloading.
Loading Modules
Configuring Mason
Because these variables are in the global namespace, users
Modifying httpd.conf
can modify them at will:
Demonstration
Time! Example: adding a directory to the front of @INC (this is
essentially how use lib works)
Example: deleting an entry in %INC to force reloading of a
module
27. Loading Modules
Module
Versioning Perl uses two arrays, @INC and %INC, to keep track of
Theron
Stanford
module loading.
@INC is a list of directories Perl searches in order to find
The Challenge
the module.
The Idea
The Keys to %INC is a hash that stores the complete path of the loaded
the Solution
Declaring Packages
module. This prevents reloading.
Loading Modules
Configuring Mason
Because these variables are in the global namespace, users
Modifying httpd.conf
can modify them at will:
Demonstration
Time! Example: adding a directory to the front of @INC (this is
essentially how use lib works)
Example: deleting an entry in %INC to force reloading of a
module
28. Loading Modules
Module
Versioning Perl uses two arrays, @INC and %INC, to keep track of
Theron
Stanford
module loading.
@INC is a list of directories Perl searches in order to find
The Challenge
the module.
The Idea
The Keys to %INC is a hash that stores the complete path of the loaded
the Solution
Declaring Packages
module. This prevents reloading.
Loading Modules
Configuring Mason
Because these variables are in the global namespace, users
Modifying httpd.conf
can modify them at will:
Demonstration
Time! Example: adding a directory to the front of @INC (this is
essentially how use lib works)
Example: deleting an entry in %INC to force reloading of a
module
29. Loading Modules
Module
Versioning Perl uses two arrays, @INC and %INC, to keep track of
Theron
Stanford
module loading.
@INC is a list of directories Perl searches in order to find
The Challenge
the module.
The Idea
The Keys to %INC is a hash that stores the complete path of the loaded
the Solution
Declaring Packages
module. This prevents reloading.
Loading Modules
Configuring Mason
Because these variables are in the global namespace, users
Modifying httpd.conf
can modify them at will:
Demonstration
Time! Example: adding a directory to the front of @INC (this is
essentially how use lib works)
Example: deleting an entry in %INC to force reloading of a
module
30. Loading Modules
Module
Versioning Perl uses two arrays, @INC and %INC, to keep track of
Theron
Stanford
module loading.
@INC is a list of directories Perl searches in order to find
The Challenge
the module.
The Idea
The Keys to %INC is a hash that stores the complete path of the loaded
the Solution
Declaring Packages
module. This prevents reloading.
Loading Modules
Configuring Mason
Because these variables are in the global namespace, users
Modifying httpd.conf
can modify them at will:
Demonstration
Time! Example: adding a directory to the front of @INC (this is
essentially how use lib works)
Example: deleting an entry in %INC to force reloading of a
module
31. Manipulating @INC
Module
Versioning @INC is not limited to directory paths.
Theron
Stanford
For our purposes, it is enough to know that @INC can
contain a subroutine reference.
The Challenge
Arguments: a reference to itself and the name of the file
The Idea
to be loaded.
The Keys to
the Solution
Return values: a list, whose first element is a filehandle to
Declaring Packages read the file from
Loading Modules
Configuring Mason
(If an empty list or undef is returned, the next value in
Modifying httpd.conf
@INC is considered.)
Demonstration
Time! In this case, the corresponding entry in %INC is set to the
subroutine reference, though the subroutine could change
this
32. Manipulating @INC
Module
Versioning @INC is not limited to directory paths.
Theron
Stanford
For our purposes, it is enough to know that @INC can
contain a subroutine reference.
The Challenge
Arguments: a reference to itself and the name of the file
The Idea
to be loaded.
The Keys to
the Solution
Return values: a list, whose first element is a filehandle to
Declaring Packages read the file from
Loading Modules
Configuring Mason
(If an empty list or undef is returned, the next value in
Modifying httpd.conf
@INC is considered.)
Demonstration
Time! In this case, the corresponding entry in %INC is set to the
subroutine reference, though the subroutine could change
this
33. Manipulating @INC
Module
Versioning @INC is not limited to directory paths.
Theron
Stanford
For our purposes, it is enough to know that @INC can
contain a subroutine reference.
The Challenge
Arguments: a reference to itself and the name of the file
The Idea
to be loaded.
The Keys to
the Solution
Return values: a list, whose first element is a filehandle to
Declaring Packages read the file from
Loading Modules
Configuring Mason
(If an empty list or undef is returned, the next value in
Modifying httpd.conf
@INC is considered.)
Demonstration
Time! In this case, the corresponding entry in %INC is set to the
subroutine reference, though the subroutine could change
this
34. Manipulating @INC
Module
Versioning @INC is not limited to directory paths.
Theron
Stanford
For our purposes, it is enough to know that @INC can
contain a subroutine reference.
The Challenge
Arguments: a reference to itself and the name of the file
The Idea
to be loaded.
The Keys to
the Solution
Return values: a list, whose first element is a filehandle to
Declaring Packages read the file from
Loading Modules
Configuring Mason
(If an empty list or undef is returned, the next value in
Modifying httpd.conf
@INC is considered.)
Demonstration
Time! In this case, the corresponding entry in %INC is set to the
subroutine reference, though the subroutine could change
this
35. Manipulating @INC
Module
Versioning @INC is not limited to directory paths.
Theron
Stanford
For our purposes, it is enough to know that @INC can
contain a subroutine reference.
The Challenge
Arguments: a reference to itself and the name of the file
The Idea
to be loaded.
The Keys to
the Solution
Return values: a list, whose first element is a filehandle to
Declaring Packages read the file from
Loading Modules
Configuring Mason
(If an empty list or undef is returned, the next value in
Modifying httpd.conf
@INC is considered.)
Demonstration
Time! In this case, the corresponding entry in %INC is set to the
subroutine reference, though the subroutine could change
this
36. How @INC Helps
Module
Versioning We unshift a subroutine reference onto the front of
Theron
Stanford
@INC.
This subroutine will inspect the filename and act according
The Challenge
to one of three cases.
The Idea
The Keys to
the Solution
Declaring Packages
Loading Modules
Configuring Mason
Modifying httpd.conf
Demonstration
Time!
37. How @INC Helps
Module
Versioning We unshift a subroutine reference onto the front of
Theron
Stanford
@INC.
This subroutine will inspect the filename and act according
The Challenge
to one of three cases.
The Idea
The Keys to
the Solution
Declaring Packages
Loading Modules
Configuring Mason
Modifying httpd.conf
Demonstration
Time!
38. Filename Begins with PerlMongers/
Module
Versioning If the filename begins with PerlMongers/:
Theron Slurp in the modules residing in
Stanford
perlmongers/pathn/lib/perl5 for each n.
The Challenge Replace references to PerlMongers:: with
The Idea pathnPerlMongers:: as appropriate.
The Keys to Concatenate the text of these two modified modules and
the Solution
Declaring Packages
place it in a lexical string variable, $source.
Loading Modules Open the string variable as a filehandle using the syntax
Configuring Mason
Modifying httpd.conf
open $fh, ’<’, $source;
Demonstration and return the filehandle.
Time!
39. Filename Begins with PerlMongers/
Module
Versioning If the filename begins with PerlMongers/:
Theron Slurp in the modules residing in
Stanford
perlmongers/pathn/lib/perl5 for each n.
The Challenge Replace references to PerlMongers:: with
The Idea pathnPerlMongers:: as appropriate.
The Keys to Concatenate the text of these two modified modules and
the Solution
Declaring Packages
place it in a lexical string variable, $source.
Loading Modules Open the string variable as a filehandle using the syntax
Configuring Mason
Modifying httpd.conf
open $fh, ’<’, $source;
Demonstration and return the filehandle.
Time!
40. Filename Begins with PerlMongers/
Module
Versioning If the filename begins with PerlMongers/:
Theron Slurp in the modules residing in
Stanford
perlmongers/pathn/lib/perl5 for each n.
The Challenge Replace references to PerlMongers:: with
The Idea pathnPerlMongers:: as appropriate.
The Keys to Concatenate the text of these two modified modules and
the Solution
Declaring Packages
place it in a lexical string variable, $source.
Loading Modules Open the string variable as a filehandle using the syntax
Configuring Mason
Modifying httpd.conf
open $fh, ’<’, $source;
Demonstration and return the filehandle.
Time!
41. Filename Begins with PerlMongers/
Module
Versioning If the filename begins with PerlMongers/:
Theron Slurp in the modules residing in
Stanford
perlmongers/pathn/lib/perl5 for each n.
The Challenge Replace references to PerlMongers:: with
The Idea pathnPerlMongers:: as appropriate.
The Keys to Concatenate the text of these two modified modules and
the Solution
Declaring Packages
place it in a lexical string variable, $source.
Loading Modules Open the string variable as a filehandle using the syntax
Configuring Mason
Modifying httpd.conf
open $fh, ’<’, $source;
Demonstration and return the filehandle.
Time!
42. Filename Begins with PerlMongers/
Module
Versioning If the filename begins with PerlMongers/:
Theron Slurp in the modules residing in
Stanford
perlmongers/pathn/lib/perl5 for each n.
The Challenge Replace references to PerlMongers:: with
The Idea pathnPerlMongers:: as appropriate.
The Keys to Concatenate the text of these two modified modules and
the Solution
Declaring Packages
place it in a lexical string variable, $source.
Loading Modules Open the string variable as a filehandle using the syntax
Configuring Mason
Modifying httpd.conf
open $fh, ’<’, $source;
Demonstration and return the filehandle.
Time!
43. Code for Filenames Perlmongers/*
Module
Versioning
open $fh, "/usr/local/perlmongers/path1/lib/perl5/$filename";
Theron
Stanford $s = <$fh>;
close $fh;
The Challenge $s =~ s"bPerlMongers::"path1PerlMongers::"g;
The Idea $source .= $s;
The Keys to $INC{"path1$filename"}
the Solution
= "/usr/local/perlmongers/path1/lib/perl5/$filename";
Declaring Packages
Loading Modules open $fh, "/usr/local/perlmongers/path2/lib/perl5/$filename";
Configuring Mason
$s = <$fh>;
Modifying httpd.conf
close $fh;
Demonstration
Time! $s =~ s"bPerlMongers::"path2PerlMongers::"g;
$source .= $s;
$INC{"path2$filename"}
= "/usr/local/perlmongers/path2/lib/perl5/$filename";
open $fh, ’<’, $source;
return $fh;
44. Filename Begins with pathnPerlMongers/
Module
Versioning If the filename begins with pathnPerlMongers/, then we
Theron know that we’ve hit a use statement in a modified
Stanford
PerlMongers::* module:
The Challenge Given pathnPerlMongers/Module.pm, slurp in
The Idea perlmongers/pathn/lib/perl5/PerlMongers/
The Keys to Module.pm.
the Solution
Declaring Packages
Replace references to PerlMongers:: with
Loading Modules pathnPerlMongers:: as appropriate.
Configuring Mason
Modifying httpd.conf
Place the module code in a lexical string variable,
Demonstration $source.
Time! Open the string variable as a filehandle using the syntax
open $fh, ’<’, $source;
and return the filehandle.
45. Filename Begins with pathnPerlMongers/
Module
Versioning If the filename begins with pathnPerlMongers/, then we
Theron know that we’ve hit a use statement in a modified
Stanford
PerlMongers::* module:
The Challenge Given pathnPerlMongers/Module.pm, slurp in
The Idea perlmongers/pathn/lib/perl5/PerlMongers/
The Keys to Module.pm.
the Solution
Declaring Packages
Replace references to PerlMongers:: with
Loading Modules pathnPerlMongers:: as appropriate.
Configuring Mason
Modifying httpd.conf
Place the module code in a lexical string variable,
Demonstration $source.
Time! Open the string variable as a filehandle using the syntax
open $fh, ’<’, $source;
and return the filehandle.
46. Filename Begins with pathnPerlMongers/
Module
Versioning If the filename begins with pathnPerlMongers/, then we
Theron know that we’ve hit a use statement in a modified
Stanford
PerlMongers::* module:
The Challenge Given pathnPerlMongers/Module.pm, slurp in
The Idea perlmongers/pathn/lib/perl5/PerlMongers/
The Keys to Module.pm.
the Solution
Declaring Packages
Replace references to PerlMongers:: with
Loading Modules pathnPerlMongers:: as appropriate.
Configuring Mason
Modifying httpd.conf
Place the module code in a lexical string variable,
Demonstration $source.
Time! Open the string variable as a filehandle using the syntax
open $fh, ’<’, $source;
and return the filehandle.
47. Filename Begins with pathnPerlMongers/
Module
Versioning If the filename begins with pathnPerlMongers/, then we
Theron know that we’ve hit a use statement in a modified
Stanford
PerlMongers::* module:
The Challenge Given pathnPerlMongers/Module.pm, slurp in
The Idea perlmongers/pathn/lib/perl5/PerlMongers/
The Keys to Module.pm.
the Solution
Declaring Packages
Replace references to PerlMongers:: with
Loading Modules pathnPerlMongers:: as appropriate.
Configuring Mason
Modifying httpd.conf
Place the module code in a lexical string variable,
Demonstration $source.
Time! Open the string variable as a filehandle using the syntax
open $fh, ’<’, $source;
and return the filehandle.
48. Filename Begins with pathnPerlMongers/
Module
Versioning If the filename begins with pathnPerlMongers/, then we
Theron know that we’ve hit a use statement in a modified
Stanford
PerlMongers::* module:
The Challenge Given pathnPerlMongers/Module.pm, slurp in
The Idea perlmongers/pathn/lib/perl5/PerlMongers/
The Keys to Module.pm.
the Solution
Declaring Packages
Replace references to PerlMongers:: with
Loading Modules pathnPerlMongers:: as appropriate.
Configuring Mason
Modifying httpd.conf
Place the module code in a lexical string variable,
Demonstration $source.
Time! Open the string variable as a filehandle using the syntax
open $fh, ’<’, $source;
and return the filehandle.
49. Code for Filenames path1PerlMongers/*
Module
Versioning
$filename =~ s"^path1"";
Theron
Stanford open $fh, "/usr/local/perlmongers/path1/lib/perl5/$filename";
$s = <$fh>;
The Challenge close $fh;
The Idea $s =~ s"bPerlMongers::"path1PerlMongers::"g;
The Keys to $source .= $s;
the Solution
$INC{"path1$filename"}
Declaring Packages
Loading Modules = "/usr/local/perlmongers/path1/lib/perl5/$filename";
Configuring Mason
$INC{"$filename"} = $coderef;
Modifying httpd.conf
open $fh, ’<’, $source;
Demonstration
Time! return $fh;
50. Other Filenames
Module
Versioning In all other cases, the module is not one of ours, and the
Theron
Stanford
subroutine returns undef.
Perl then continues to work its way through @INC to find
The Challenge
the module.
The Idea
The Keys to
the Solution
Declaring Packages
Loading Modules
Configuring Mason
Modifying httpd.conf
Demonstration
Time!
51. Other Filenames
Module
Versioning In all other cases, the module is not one of ours, and the
Theron
Stanford
subroutine returns undef.
Perl then continues to work its way through @INC to find
The Challenge
the module.
The Idea
The Keys to
the Solution
Declaring Packages
Loading Modules
Configuring Mason
Modifying httpd.conf
Demonstration
Time!
52. Handler Specifics
Module
Versioning Each path needs its own Mason handler so that it will pick
Theron
Stanford
up the components from the right directory. Once the
handler is written, it need never be modified again.
The Challenge
The comp_root and data_dir parameters sent to
The Idea
The Keys to
HTML::Mason::ApacheHandler->new() will take care of
the Solution this.
Declaring Packages
Loading Modules
Configuring Mason
Modifying httpd.conf
Demonstration
Time!
53. Handler Specifics
Module
Versioning Each path needs its own Mason handler so that it will pick
Theron
Stanford
up the components from the right directory. Once the
handler is written, it need never be modified again.
The Challenge
The comp_root and data_dir parameters sent to
The Idea
The Keys to
HTML::Mason::ApacheHandler->new() will take care of
the Solution this.
Declaring Packages
Loading Modules
Configuring Mason
Modifying httpd.conf
Demonstration
Time!
54. Munging Mason Files
Module
Versioning The Mason files are filled with use statements for the
Theron
Stanford
originally named Perl modules; thus, these files must be
changed to use the renamed modules.
The Challenge
Fortunately, Mason has a way to do this: the preprocess
The Idea
The Keys to
parameter sent to
the Solution HTML::Mason::ApacheHandler->new().
Declaring Packages
Loading Modules
Configuring Mason
The value of preprocess is a subroutine reference. The
Modifying httpd.conf
subroutine takes a reference to a string containing the
Demonstration
Time!
contents of the Mason file; the subroutine can then modify
the contents in place before they are loaded by Mason.
55. Munging Mason Files
Module
Versioning The Mason files are filled with use statements for the
Theron
Stanford
originally named Perl modules; thus, these files must be
changed to use the renamed modules.
The Challenge
Fortunately, Mason has a way to do this: the preprocess
The Idea
The Keys to
parameter sent to
the Solution HTML::Mason::ApacheHandler->new().
Declaring Packages
Loading Modules
Configuring Mason
The value of preprocess is a subroutine reference. The
Modifying httpd.conf
subroutine takes a reference to a string containing the
Demonstration
Time!
contents of the Mason file; the subroutine can then modify
the contents in place before they are loaded by Mason.
56. Munging Mason Files
Module
Versioning The Mason files are filled with use statements for the
Theron
Stanford
originally named Perl modules; thus, these files must be
changed to use the renamed modules.
The Challenge
Fortunately, Mason has a way to do this: the preprocess
The Idea
The Keys to
parameter sent to
the Solution HTML::Mason::ApacheHandler->new().
Declaring Packages
Loading Modules
Configuring Mason
The value of preprocess is a subroutine reference. The
Modifying httpd.conf
subroutine takes a reference to a string containing the
Demonstration
Time!
contents of the Mason file; the subroutine can then modify
the contents in place before they are loaded by Mason.
57. Munging Mason Files: The Code
Module
Versioning
package PerlMongers::Handler;
Theron
Stanford use strict;
use HTML::Mason::ApacheHandler;
The Challenge
The Idea my $ah = HTML::Mason::ApacheHandler->new(
The Keys to comp_root => ’/usr/local/perlmongers/path1/www’,
the Solution
data_dir => ’/usr/local/perlmongers/path1/www/mason’,
Declaring Packages
Loading Modules static_source => 1,
Configuring Mason
error_mode => ’fatal’,
Modifying httpd.conf
preprocess => sub {
Demonstration
Time! my $ref = shift;
$$ref =~ s"bPerlMongers::"path1PerlMongers::"g;
},
);
sub handler {
my $r = shift;
return $ah->handle_request($r);
}
58. Configuring Apache
Module
Versioning The Apache httpd.conf file must be configured to handle the
Theron
Stanford
following:
Inserting the subroutine reference at the beginning of @INC
The Challenge
to load the PerlMongers::* modules correctly. This is
The Idea
The Keys to
done by putting this code in a small startup script,
the Solution startup.pl, and using a PerlRequire directive to
Declaring Packages
Loading Modules include it.
Configuring Mason
Modifying httpd.conf Note: The order of code compilation is not always what
Demonstration you expect in Perl and mod_perl. Thus, you should put
Time!
the code inside startup.pl inside a BEGIN block. (I
speak from direct experience.)
Pointing the URLs to the correct directories, including the
correct handlers for the Mason code.
59. Configuring Apache
Module
Versioning The Apache httpd.conf file must be configured to handle the
Theron
Stanford
following:
Inserting the subroutine reference at the beginning of @INC
The Challenge
to load the PerlMongers::* modules correctly. This is
The Idea
The Keys to
done by putting this code in a small startup script,
the Solution startup.pl, and using a PerlRequire directive to
Declaring Packages
Loading Modules include it.
Configuring Mason
Modifying httpd.conf Note: The order of code compilation is not always what
Demonstration you expect in Perl and mod_perl. Thus, you should put
Time!
the code inside startup.pl inside a BEGIN block. (I
speak from direct experience.)
Pointing the URLs to the correct directories, including the
correct handlers for the Mason code.
60. Code for Loading Modules
Module
Versioning
PerlRequire /usr/local/perlmongers/lib/perl5/startup.pl
Theron
Stanford
<Perl>
The Challenge use lib qw(/usr/local/perlmongers/lib/perl5);
The Idea </Perl>
The Keys to
the Solution
## Reversing the order of these statements
Declaring Packages
Loading Modules ## gives the following error:
Configuring Mason
##
Modifying httpd.conf
## Subroutine requiresub redefined
Demonstration
Time! ## at /usr/local/perlmongers/lib/perl5/startup.pl line 2.
##
## Any insights as to why would be greatly appreciated!
61. Code for Path-Specific Content
Module
Versioning
PerlModule PerlMongers::Handler
Theron
Stanford
Alias /path1 /usr/local/perlmongers/path1/www
The Challenge Alias /path2 /usr/local/perlmongers/path2/www
The Idea
The Keys to <Location /path1>
the Solution
SetHandler perl-script
Declaring Packages
Loading Modules PerlHandler path1PerlMongers::Handler
Configuring Mason
</Location>
Modifying httpd.conf
<Location /path2>
Demonstration
Time! SetHandler perl-script
PerlHandler path2PerlMongers::Handler
</Location>
62. Demonstration Time!
Module
Versioning
Theron
Stanford
The Challenge
The Idea
The Keys to
the Solution
Declaring Packages
Loading Modules
Configuring Mason
Modifying httpd.conf
Demonstration
Time!