You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Steve Hay <St...@planit.com> on 2007/08/10 17:35:01 UTC

Using perl source code filter in mod_perl2

Is it possible to use perl source code filters (like Filter::Util::Call)
in scripts that are run via ModPerl::Registry?

If I place the following module somewhere in my @INC path:

Hello2Goodbye.pm
----------------
package Hello2Goodbye;
use Filter::Util::Call;
sub import {
	my $type = shift;
	filter_add(bless []);
}
sub filter {
	my $self = shift;
	my $status;
	s/Hello/Goodbye/g if ($status = filter_read()) > 0;
	return $status;
}
1;

and then run the following script via ModPerl::Registry:

filtertest1.pl
--------------
use Hello2Goodbye;
print "Content-Type: text/html\n\n";
print "<HTML><HEAD><TITLE>Filter Test</TITLE></HEAD><BODY>\n";
print "<H1>Hello, world.</H1>\n";
print "</BODY></HTML>\n";

then I get the output "Hello, world." rather than "Goodbye, world.",
i.e. the source code didn't get filtered.

I can make it work by rewriting the script as:

filtertest2.pl
--------------
use FilterTest;
FilterTest::greeting();

where the FilterTest module contains:

FilterTest.pm
-------------
use Hello2Goodbye;
package FilterTest;
sub greeting() {
	print "Content-Type: text/html\n\n";
	print "<HTML><HEAD><TITLE>Filter Test</TITLE></HEAD><BODY>\n";
	print "<H1>Hello, world.</H1>\n";
	print "</BODY></HTML>\n";
}
1;

but I'd really like the scripts themselves to be source-filtered too,
not just the modules that they use.

Any ideas?

Re: Using perl source code filter in mod_perl2

Posted by Perrin Harkins <pe...@elem.com>.
On 8/10/07, Steve Hay <St...@planit.com> wrote:
> Is it possible to use perl source code filters (like Filter::Util::Call)
> in scripts that are run via ModPerl::Registry?

I don't think that can work because of the way Registry modifies your
code.  Try using handlers instead of Registry scripts, or make your
own subclass of Registry that runs the source filter manually on the
code before turning it into a sub.

- Perrin