<?xml version="1.0" encoding="windows-1252"?>
<node id="562748" title="Re: mod_perl / Apache::Registry accidental closures" created="2006-07-20 20:51:21" updated="2006-07-20 16:51:21">
<type id="11">
note</type>
<author id="502468">
imp</author>
<data>
<field name="doctext">
I'm including an abuse of the mechanism in a separate comment,  because it is not directly related to the tutorial. 
&lt;p&gt;
&lt;b&gt;This is a bad thing to do, and any programmer who inherits the project from you will do horrible things to you  if you use this trick&lt;/b&gt;

&lt;readmore title="Hidden to avoid eye trauma"&gt;
&lt;code&gt;
use strict;
use warnings;

my $foo = 5;

print "Content-type: text/plain\n";
print "Content-disposition: inline; filename=foo.txt\n\n";

printf "Package: %s\n", __PACKAGE__;
printf "[%s] Before: %s\n", $$, $foo;
goodness(5);
printf "[%s] After: %s\n", $$, $foo;


}

sub goodness {
    my $val = shift;
    printf "[%s] goodness: %s\n", $$, $foo;
    $foo += $val;
&lt;/code&gt;

Which Apache::Registry will turn into this:
&lt;code&gt;
package Apache::ROOTfoo_2ecom::test_2epl;
use Apache qw(exit);
sub handler {
#line 1 /www/foo.com/test.pl
use strict;
use warnings;

my $foo = 5;

print "Content-type: text/plain\n";
print "Content-disposition: inline; filename=foo.txt\n\n";

printf "Package: %s\n", __PACKAGE__;
printf "[%s] Before: %s\n", $$, $foo;
goodness(5);
printf "[%s] After: %s\n", $$, $foo;


}

sub goodness {
    my $val = shift;
    printf "[%s] goodness: %s\n", $$, $foo;
    $foo += $val;
}
&lt;/code&gt;

By adding an extra '}' before the &lt;i&gt;sub goodness&lt;/i&gt; declaration we are really closing &lt;i&gt;sub handler {&lt;/i&gt;, and then at the end of the script we leave off the closing '}', since one will be added by Apache::Registry.

&lt;/readmore&gt;
</field>
<field name="root_node">
562746</field>
<field name="parent_node">
562746</field>
</data>
</node>
