http://www.perlmonks.org?node_id=562748


in reply to mod_perl / Apache::Registry accidental closures

I'm including an abuse of the mechanism in a separate comment, because it is not directly related to the tutorial.

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

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;
Which Apache::Registry will turn into this:
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; }
By adding an extra '}' before the sub goodness declaration we are really closing sub handler {, and then at the end of the script we leave off the closing '}', since one will be added by Apache::Registry.

Replies are listed 'Best First'.
Re^2: mod_perl / Apache::Registry accidental closures
by rhesa (Vicar) on Jul 21, 2006 at 01:31 UTC
    My eyes! Ze goggles, zey do nothing!1!!

    Please, please, hide this behind spoiler tags :^)