This file is indexed.

/usr/share/doc/libapache2-mod-perl2-doc/docs/2.0/api/Apache2/PerlSections.html is in libapache2-mod-perl2-doc 2.0.8+httpd24-r1449661-6ubuntu2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
<?xml version="1.0" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rev="made" href="mailto:root@localhost" />
</head>

<body style="background-color: white">



<ul id="index">
  <li><a href="#NAME">NAME</a></li>
  <li><a href="#Synopsis">Synopsis</a></li>
  <li><a href="#Description">Description</a></li>
  <li><a href="#API">API</a>
    <ul>
      <li><a href="#server">server</a></li>
    </ul>
  </li>
  <li><a href="#PerlConfig-and-PerlConfig">@PerlConfig and $PerlConfig</a></li>
  <li><a href="#Configuration-Variables">Configuration Variables</a>
    <ul>
      <li><a href="#Apache2::PerlSections::Save">$Apache2::PerlSections::Save</a></li>
    </ul>
  </li>
  <li><a href="#PerlSections-Dumping">PerlSections Dumping</a>
    <ul>
      <li><a href="#Apache2::PerlSections-dump">Apache2::PerlSections-&gt;dump</a></li>
      <li><a href="#Apache2::PerlSections-store">Apache2::PerlSections-&gt;store</a></li>
    </ul>
  </li>
  <li><a href="#Advanced-API">Advanced API</a></li>
  <li><a href="#Verifying-Perl-Sections">Verifying &lt;Perl&gt; Sections</a></li>
  <li><a href="#Bugs">Bugs</a>
    <ul>
      <li><a href="#Perl-directive-missing-closing">&lt;Perl&gt; directive missing closing &#39;&gt;&#39;</a></li>
      <li><a href="#Perl-...-was-not-closed">&lt;Perl&gt;[...]&gt; was not closed.</a></li>
    </ul>
  </li>
  <li><a href="#See-Also">See Also</a></li>
  <li><a href="#Copyright">Copyright</a></li>
  <li><a href="#Authors">Authors</a></li>
</ul>

<h1 id="NAME">NAME</h1>

<p>Apache2::PerlSections - write Apache configuration files in Perl</p>

<h1 id="Synopsis">Synopsis</h1>

<pre><code>  &lt;Perl&gt;
  @PerlModule = qw(Mail::Send Devel::Peek);
  
  #run the server as whoever starts it
  $User  = getpwuid(&gt;) || &gt;;
  $Group = getgrgid()) || );
  
  $ServerAdmin = $User;
  
  &lt;/Perl&gt;</code></pre>

<h1 id="Description">Description</h1>

<p>With <code>&lt;Perl&gt;</code>...<code>&lt;/Perl&gt;</code> sections, it is possible to configure your server entirely in Perl.</p>

<p><code>&lt;Perl&gt;</code> sections can contain <i>any</i> and as much Perl code as you wish. These sections are compiled into a special package whose symbol table mod_perl can then walk and grind the names and values of Perl variables/structures through the Apache core configuration gears.</p>

<p>Block sections such as <code>&lt;Location&gt;</code>..<code>&lt;/Location&gt;</code> are represented in a <code>%Location</code> hash, e.g.:</p>

<pre><code>  &lt;Perl&gt;
  $Location{&quot;/~dougm/&quot;} = {
    AuthUserFile   =&gt; &#39;/tmp/htpasswd&#39;,
    AuthType       =&gt; &#39;Basic&#39;,
    AuthName       =&gt; &#39;test&#39;,
    DirectoryIndex =&gt; [qw(index.html index.htm)],
    Limit          =&gt; {
        &quot;GET POST&quot;    =&gt; {
            require =&gt; &#39;user dougm&#39;,
        }
    },
  };
  &lt;/Perl&gt;</code></pre>

<p>If an Apache directive can take two or three arguments you may push strings (the lowest number of arguments will be shifted off the <code>@list</code>) or use an array reference to handle any number greater than the minimum for that directive:</p>

<pre><code>  push @Redirect, &quot;/foo&quot;, &quot;http://www.foo.com/&quot;;
  
  push @Redirect, &quot;/imdb&quot;, &quot;http://www.imdb.com/&quot;;
  
  push @Redirect, [qw(temp &quot;/here&quot; &quot;http://www.there.com&quot;)];</code></pre>

<p>Other section counterparts include <code>%VirtualHost</code>, <code>%Directory</code> and <code>%Files</code>.</p>

<p>To pass all environment variables to the children with a single configuration directive, rather than listing each one via <code>PassEnv</code> or <code>PerlPassEnv</code>, a <code>&lt;Perl&gt;</code> section could read in a file and:</p>

<pre><code>  push @PerlPassEnv, [$key =&gt; $val];</code></pre>

<p>or</p>

<pre><code>  Apache2-&gt;httpd_conf(&quot;PerlPassEnv $key $val&quot;);</code></pre>

<p>These are somewhat simple examples, but they should give you the basic idea. You can mix in any Perl code you desire. See <i>eg/httpd.conf.pl</i> and <i>eg/perl_sections.txt</i> in the mod_perl distribution for more examples.</p>

<p>Assume that you have a cluster of machines with similar configurations and only small distinctions between them: ideally you would want to maintain a single configuration file, but because the configurations aren&#39;t <i>exactly</i> the same (e.g. the <code>ServerName</code> directive) it&#39;s not quite that simple.</p>

<p><code>&lt;Perl&gt;</code> sections come to rescue. Now you have a single configuration file and the full power of Perl to tweak the local configuration. For example to solve the problem of the <code>ServerName</code> directive you might have this <code>&lt;Perl&gt;</code> section:</p>

<pre><code>  &lt;Perl&gt;
  $ServerName = `hostname`;
  &lt;/Perl&gt;</code></pre>

<p>For example if you want to allow personal directories on all machines except the ones whose names start with <i>secure</i>:</p>

<pre><code>  &lt;Perl&gt;
  $ServerName = `hostname`;
  if ($ServerName !~ /^secure/) {
      $UserDir = &quot;public.html&quot;;
  }
  else {
      $UserDir = &quot;DISABLED&quot;;
  }
  &lt;/Perl&gt;</code></pre>

<h1 id="API">API</h1>

<p><code>Apache2::PerlSections</code> provides the following functions and/or methods:</p>

<h2 id="server"><code>server</code></h2>

<p>Get the current server&#39;s object for the &lt;Perl&gt; section</p>

<pre><code>  &lt;Perl&gt;
    $s = Apache2::PerlSections-&gt;server();
  &lt;/Perl&gt;</code></pre>

<dl>

<dt id="obj:-Apache2::PerlSections-class-name">obj: <code>Apache2::PerlSections</code> (class name)</dt>
<dd>

</dd>
<dt id="ret:-s-Apache2::ServerRec-object">ret: <code>$s</code> ( <code><a>Apache2::ServerRec object</a></code> )</dt>
<dd>

</dd>
<dt id="since:-2.0.03">since: 2.0.03</dt>
<dd>

</dd>
</dl>

<h1 id="PerlConfig-and-PerlConfig"><code>@PerlConfig</code> and <code>$PerlConfig</code></h1>

<p>This array and scalar can be used to introduce literal configuration into the apache configuration. For example:</p>

<pre><code>  push @PerlConfig, &#39;Alias /foo /bar&#39;;</code></pre>

<p>Or: $PerlConfig .= &quot;Alias /foo /bar\n&quot;;</p>

<p>See also <code><a>$r-&gt;add_config</a></code></p>

<h1 id="Configuration-Variables">Configuration Variables</h1>

<p>There are a few variables that can be set to change the default behaviour of <code>&lt;Perl&gt;</code> sections.</p>

<h2 id="Apache2::PerlSections::Save"><code>$Apache2::PerlSections::Save</code></h2>

<p>Each <code>&lt;Perl&gt;</code> section is evaluated in its unique namespace, by default residing in a sub-namespace of <code>Apache2::ReadConfig::</code>, therefore any local variables will end up in that namespace. For example if a <code>&lt;Perl&gt;</code> section happened to be in file <i>/tmp/httpd.conf</i> starting on line 20, the namespace: <code>Apache2::ReadConfig::tmp::httpd_conf::line_20</code> will be used. Now if it had:</p>

<pre><code>  &lt;Perl&gt;
    $foo     = 5;
    my $bar  = 6;
    $My::tar = 7;
  &lt;/Perl&gt;</code></pre>

<p>The local global variable <code>$foo</code> becomes <code>$Apache2::ReadConfig::tmp::httpd_conf::line_20::foo</code>, the other variable remain where they are.</p>

<p>By default, the namespace in which <code>&lt;Perl&gt;</code> sections are evaluated is cleared after each block closes. In our example nuking <code>$Apache2::ReadConfig::tmp::httpd_conf::line_20::foo</code>, leaving the rest untouched.</p>

<p>By setting <code>$Apache2::PerlSections::Save</code> to a true value, the content of those namespaces will be preserved and will be available for inspection by <code><a>Apache2::Status</a></code> and <code><a href="#C_Apache2__PerlSections_E_gt_dump_">Apache2::PerlSections-&gt;dump</a></code> In our example <code>$Apache2::ReadConfig::tmp::httpd_conf::line_20::foo</code> will still be accessible from other perl code, after the <code>&lt;Perl&gt;</code> section was parsed.</p>

<h1 id="PerlSections-Dumping">PerlSections Dumping</h1>

<h2 id="Apache2::PerlSections-dump"><code>Apache2::PerlSections-&gt;dump</code></h2>

<p>This method will dump out all the configuration variables mod_perl will be feeding to the apache config gears. The output is suitable to read back in via <code>eval</code>.</p>

<pre><code>  my $dump = Apache2::PerlSections-&gt;dump;</code></pre>

<dl>

<dt id="ret:-dump-string-undef">ret: <code>$dump</code> ( string / <code>undef</code> )</dt>
<dd>

<p>A string dump of all the Perl code encountered in &lt;Perl&gt; blocks, suitable to be read back via <code>eval</code></p>

</dd>
</dl>

<p>For example:</p>

<pre><code>  &lt;Perl&gt;
  
  $Apache2::PerlSections::Save = 1;
  
  $Listen = 8529;
  
  $Location{&quot;/perl&quot;} = {
     SetHandler =&gt; &quot;perl-script&quot;,
     PerlHandler =&gt; &quot;ModPerl::Registry&quot;,
     Options =&gt; &quot;ExecCGI&quot;,
  };
  
  @DirectoryIndex = qw(index.htm index.html);
  
  $VirtualHost{&quot;www.foo.com&quot;} = {
     DocumentRoot =&gt; &quot;/tmp/docs&quot;,
     ErrorLog =&gt; &quot;/dev/null&quot;,
     Location =&gt; {
       &quot;/&quot; =&gt; {
         Allowoverride =&gt; &#39;All&#39;,
         Order =&gt; &#39;deny,allow&#39;,
         Deny  =&gt; &#39;from all&#39;,
         Allow =&gt; &#39;from foo.com&#39;,
       },
     },
  };
  &lt;/Perl&gt;
  
  &lt;Perl&gt;
  print Apache2::PerlSections-&gt;dump;
  &lt;/Perl&gt;</code></pre>

<p>This will print something like this:</p>

<pre><code>  $Listen = 8529;
  
  @DirectoryIndex = (
    &#39;index.htm&#39;,
    &#39;index.html&#39;
  );
  
  $Location{&#39;/perl&#39;} = (
      PerlHandler =&gt; &#39;Apache2::Registry&#39;,
      SetHandler =&gt; &#39;perl-script&#39;,
      Options =&gt; &#39;ExecCGI&#39;
  );
  
  $VirtualHost{&#39;www.foo.com&#39;} = (
      Location =&gt; {
        &#39;/&#39; =&gt; {
          Deny =&gt; &#39;from all&#39;,
          Order =&gt; &#39;deny,allow&#39;,
          Allow =&gt; &#39;from foo.com&#39;,
          Allowoverride =&gt; &#39;All&#39;
        }
      },
      DocumentRoot =&gt; &#39;/tmp/docs&#39;,
      ErrorLog =&gt; &#39;/dev/null&#39;
  );
  
  1;
  __END__</code></pre>

<p>It is important to put the call to <code>dump</code> in it&#39;s own <code>&lt;Perl&gt;</code> section, otherwise the content of the current <code>&lt;Perl&gt;</code> section will not be dumped.</p>

<h2 id="Apache2::PerlSections-store"><code>Apache2::PerlSections-&gt;store</code></h2>

<p>This method will call the <code>dump</code> method, writing the output to a file, suitable to be pulled in via <code>require</code> or <code>do</code>.</p>

<pre><code>  Apache2::PerlSections-&gt;store($filename);</code></pre>

<dl>

<dt id="arg1:-filename-string">arg1: <code>$filename</code> (string)</dt>
<dd>

<p>The filename to save the dump output to</p>

</dd>
<dt id="ret:-no-return-value">ret: no return value</dt>
<dd>

</dd>
</dl>

<h1 id="Advanced-API">Advanced API</h1>

<p>mod_perl 2.0 now introduces the same general concept of handlers to <code>&lt;Perl&gt;</code> sections. Apache2::PerlSections simply being the default handler for them.</p>

<p>To specify a different handler for a given perl section, an extra handler argument must be given to the section:</p>

<pre><code>  &lt;Perl handler=&quot;My::PerlSection::Handler&quot; somearg=&quot;test1&quot;&gt;
    $foo = 1;
    $bar = 2;
  &lt;/Perl&gt;</code></pre>

<p>And in My/PerlSection/Handler.pm:</p>

<pre><code>  sub My::Handler::handler : handler {
      my ($self, $parms, $args) = @_;
      #do your thing!
  }</code></pre>

<p>So, when that given <code>&lt;Perl&gt;</code> block in encountered, the code within will first be evaluated, then the handler routine will be invoked with 3 arguments:</p>

<dl>

<dt id="arg1:-self">arg1: <code>$self</code></dt>
<dd>

<p>self-explanatory</p>

</dd>
<dt id="arg2:-parms-Apache2::CmdParms">arg2: <code>$parms</code> ( <code><a>Apache2::CmdParms</a></code> )</dt>
<dd>

<p><code>$parms</code> is specific for the current Container, for example, you might want to call <code>$parms-&gt;server()</code> to get the current server.</p>

</dd>
<dt id="arg3:-args-APR::Table-object">arg3: <code>$args</code> ( <code><a>APR::Table object</a></code>)</dt>
<dd>

<p>the table object of the section arguments. The 2 guaranteed ones will be:</p>

<pre><code>  $args-&gt;{&#39;handler&#39;} = &#39;My::PerlSection::Handler&#39;;
  $args-&gt;{&#39;package&#39;} = &#39;Apache2::ReadConfig&#39;;</code></pre>

<p>Other <code>name=&quot;value&quot;</code> pairs given on the <code>&lt;Perl&gt;</code> line will also be included.</p>

</dd>
</dl>

<p>At this point, it&#39;s up to the handler routing to inspect the namespace of the <code>$args</code>-&gt;{&#39;package&#39;} and chooses what to do.</p>

<p>The most likely thing to do is to feed configuration data back into apache. To do that, use Apache2::Server-&gt;add_config(&quot;directive&quot;), for example:</p>

<pre><code>  $parms-&gt;server-&gt;add_config(&quot;Alias /foo /bar&quot;);</code></pre>

<p>Would create a new alias. The source code of <code>Apache2::PerlSections</code> is a good place to look for a practical example.</p>

<h1 id="Verifying-Perl-Sections">Verifying <code>&lt;Perl&gt;</code> Sections</h1>

<p>If the <code>&lt;Perl&gt;</code> sections include no code requiring a running mod_perl, it is possible to check those from the command line. But the following trick should be used:</p>

<pre><code>  # file: httpd.conf
  &lt;Perl&gt;
  #!perl
  
  # ... code here ...
  
  __END__
  &lt;/Perl&gt;</code></pre>

<p>Now you can run:</p>

<pre><code>  % perl -c httpd.conf</code></pre>

<h1 id="Bugs">Bugs</h1>

<h2 id="Perl-directive-missing-closing">&lt;Perl&gt; directive missing closing &#39;&gt;&#39;</h2>

<p>httpd-2.0.47 had a bug in the configuration parser which caused the startup failure with the following error:</p>

<pre><code>  Starting httpd:
  Syntax error on line ... of /etc/httpd/conf/httpd.conf:
  &lt;Perl&gt; directive missing closing &#39;&gt;&#39;     [FAILED]</code></pre>

<p>This has been fixed in httpd-2.0.48. If you can&#39;t upgrade to this or a higher version, please add a space before the closing &#39;&gt;&#39; of the opening tag as a workaround. So if you had:</p>

<pre><code>  &lt;Perl&gt;
  # some code
  &lt;/Perl&gt;</code></pre>

<p>change it to be:</p>

<pre><code>  &lt;Perl &gt;
  # some code
  &lt;/Perl&gt;</code></pre>

<h2 id="Perl-...-was-not-closed">&lt;Perl&gt;[...]&gt; was not closed.</h2>

<p>On encountering a one-line &lt;Perl&gt; block, httpd&#39;s configuration parser will cause a startup failure with an error similar to this one:</p>

<pre><code>  Starting httpd:
  Syntax error on line ... of /etc/httpd/conf/httpd.conf:
  &lt;Perl&gt;use&gt; was not closed.</code></pre>

<p>If you have written a simple one-line &lt;Perl&gt; section like this one :</p>

<pre><code>  &lt;Perl&gt;use Apache::DBI;&lt;/Perl&gt;</code></pre>

<p>change it to be:</p>

<pre><code>   &lt;Perl&gt;
   use Apache::DBI;
   &lt;/Perl&gt;</code></pre>

<p>This is caused by a limitation of httpd&#39;s configuration parser and is not likely to be changed to allow one-line block like the example above. Use multi-line blocks instead.</p>

<h1 id="See-Also">See Also</h1>

<p><a>mod_perl 2.0 documentation</a>.</p>

<h1 id="Copyright">Copyright</h1>

<p>mod_perl 2.0 and its core modules are copyrighted under The Apache Software License, Version 2.0.</p>

<h1 id="Authors">Authors</h1>

<p><a>The mod_perl development team and numerous contributors</a>.</p>


</body>

</html>