File: RegistryLoader.pod

package info (click to toggle)
libapache2-mod-perl2 2.0.9~1624218-2%2Bdeb8u2
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 11,912 kB
  • ctags: 4,588
  • sloc: perl: 95,064; ansic: 14,527; makefile: 49; sh: 18
file content (192 lines) | stat: -rw-r--r-- 5,692 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
=head1 NAME

ModPerl::RegistryLoader - Compile ModPerl::RegistryCooker scripts at server startup

=head1 Synopsis

  # in startup.pl
  use ModPerl::RegistryLoader ();
  use File::Spec ();
  
  # explicit uri => filename mapping
  my $rlbb = ModPerl::RegistryLoader->new(
      package => 'ModPerl::RegistryBB',
      debug   => 1, # default 0
  );

  $rlbb->handler($uri, $filename);
  
  ###
  # uri => filename mapping using a helper function
  sub trans {
      my $uri = shift;
      $uri =~ s|^/registry/|cgi-bin/|;
      return File::Spec->catfile(Apache2::ServerUtil::server_root, $uri);
  }
  my $rl = ModPerl::RegistryLoader->new(
      package => 'ModPerl::Registry',
      trans   => \&trans,
  );
  $rl->handler($uri);
  
  ###
  $rlbb->handler($uri, $filename, $virtual_hostname);


=head1 Description

This modules allows compilation of scripts, running under packages
derived from C<ModPerl::RegistryCooker>, at server startup.  The
script's handler routine is compiled by the parent server, of which
children get a copy and thus saves some memory by initially sharing
the compiled copy with the parent and saving the overhead of script's
compilation on the first request in every httpd instance.

This module is of course useless for those running the
C<L<ModPerl::PerlRun>> handler, because the scripts get recompiled on
each request under this handler.

=head1 Methods

=over

=item new()

When creating a new C<ModPerl::RegistryLoader> object, one has to
specify which of the C<ModPerl::RegistryCooker> derived modules to
use. For example if a script is going to run under
C<ModPerl::RegistryBB> the object is initialized as:

  my $rlbb = ModPerl::RegistryLoader->new(
      package => 'ModPerl::RegistryBB',
  );

If the package is not specified C<ModPerl::Registry> is assumed:

  my $rlbb = ModPerl::RegistryLoader->new();

To turn the debugging on, set the I<debug> attribute to a true value:

  my $rlbb = ModPerl::RegistryLoader->new(
      package => 'ModPerl::RegistryBB',
      debug   => 1,
  );

Instead of specifying explicitly a filename for each uri passed to
handler(), a special attribute I<trans> can be set to a subroutine to
perform automatic remapping.

  my $rlbb = ModPerl::RegistryLoader->new(
      package => 'ModPerl::RegistryBB',
      trans   => \&trans,
  );

See the handler() item for an example of using the I<trans> attribute.

=item handler()

  $rl->handler($uri, [$filename, [$virtual_hostname]]);

The handler() method takes argument of C<uri> and optionally of
C<filename> and of C<virtual_hostname>.

URI to filename translation normally doesn't happen until HTTP request
time, so we're forced to roll our own translation. If the filename is
supplied it's used in translation.

If the filename is omitted and a C<trans> subroutine was not set in
new(), the loader will try using the C<uri> relative to the
C<ServerRoot> configuration directive.  For example:

  httpd.conf:
  -----------
  ServerRoot /usr/local/apache
  Alias /registry/ /usr/local/apache/cgi-bin/

  startup.pl:
  -----------
  use ModPerl::RegistryLoader ();
  my $rl = ModPerl::RegistryLoader->new(
      package => 'ModPerl::Registry',
  );
  # preload /usr/local/apache/cgi-bin/test.pl
  $rl->handler(/registry/test.pl);

To make the loader smarter about the URI-E<gt>filename translation,
you may provide the C<new()> method with a C<trans()> function to
translate the uri to filename.

The following example will pre-load all files ending with I<.pl> in
the I<cgi-bin> directory relative to C<ServerRoot>.

  httpd.conf:
  -----------
  ServerRoot /usr/local/apache
  Alias /registry/ /usr/local/apache/cgi-bin/

  startup.pl:
  -----------
  {
      # test the scripts pre-loading by using trans sub
      use ModPerl::RegistryLoader ();
      use File::Spec ();
      use DirHandle ();
      use strict;
  
      my $dir = File::Spec->catdir(Apache2::ServerUtil::server_root,
                                  "cgi-bin");
  
      sub trans {
          my $uri = shift; 
          $uri =~ s|^/registry/|cgi-bin/|;
          return File::Spec->catfile(Apache2::ServerUtil::server_root,
                                     $uri);
      }
  
      my $rl = ModPerl::RegistryLoader->new(
          package => "ModPerl::Registry",
          trans   => \&trans,
      );
      my $dh = DirHandle->new($dir) or die $!;
  
      for my $file ($dh->read) {
          next unless $file =~ /\.pl$/;
          $rl->handler("/registry/$file");
      }
  }

If C<$virtual_hostname> argument is passed it'll be used in the
creation of the package name the script will be compiled into for
those registry handlers that use I<namespace_from_uri()> method.  See
also the notes on C<$ModPerl::RegistryCooker::NameWithVirtualHost> in
the C<L<ModPerl::RegistryCooker>> documentation.

Also
explained in the C<L<ModPerl::RegistryLoader>> documentation, this
only has an effect at run time if
C<$ModPerl::RegistryCooker::NameWithVirtualHost> is set to true,
otherwise the C<$virtual_hostname> argument is ignored.

=back


=head1 Implementation Notes

C<ModPerl::RegistryLoader> performs a very simple job, at run time it
loads and sub-classes the module passed via the I<package> attribute
and overrides some of its functions, to emulate the run-time
environment. This allows one to preload the same script into different
registry environments.

=head1 Authors

The original C<Apache2::RegistryLoader> implemented by Doug MacEachern.

Stas Bekman did the porting to the new registry framework based on
C<ModPerl::RegistryLoader>.

=head1 SEE ALSO

C<L<ModPerl::RegistryCooker>>, C<L<ModPerl::Registry>>,
C<L<ModPerl::RegistryBB>>, C<L<ModPerl::PerlRun>>, Apache(3),
mod_perl(3)