cormanaz has asked for the wisdom of the Perl Monks concerning the following question:

Howdy Bros. I am trying to figure out how to use HTTP::Proxy to do a simple filter on a response. Based on the docs, I implemented the following code:
#!/usr/bin/perl -w use HTTP::Proxy; use HTTP::Proxy::BodyFilter::simple; use HTTP::Proxy::BodyFilter::complete; my $proxy = HTTP::Proxy->new; # pass the complete response body to our filter (in one pass) $proxy->push_filter( mime => 'text/html', response => HTTP::Proxy::BodyFilter::complete->new, response => HTTP::Proxy::BodyFilter::simple->new( sub { my ( $self, $dataref, $message, $protocol, $buffer ) = @_; $dataref =~ s/Perl/PERL/; } ) ); $proxy->start;
When I access the Wikipedia Perl page through this proxy, it delivers the page but it does not substitute Perl with PERL.

It seems like the target of the substitution statement should be the right one because related docs say "$dataref is a reference to the chunk of body data received." I also tried changing the target to ${ $_[1] } as suggested here but that doesn't work either. The debugger indicates there is nothing like HTML in either of those variables.

So what am I doing wrong?

BTW my eventual goal is to intercept the html, parse/modify it with HTML::TreeParser, then send the modded data back to the client, if that mattes. But I thought I should start with something simple.

TIA

Steve

Replies are listed 'Best First'.
Re: HTTP::Proxy::BodyFilter Shezza No Voik
by ikegami (Patriarch) on Dec 04, 2009 at 00:08 UTC

    Right now, you're attempting to modify a copy of a scalar you never do anything with. Definitely bad. According to the docs you quoted, the var doesn't even contain a string. It contains a reference to the data. You should be doing

    $$dataref =~ s/Perl/PERL/;

    Note that the above is the same as

    ${$_[1]} =~ s/Perl/PERL/;

    You probably want to use the /g modifier if you want to change more than just the first occurrence.

    $$dataref =~ s/Perl/PERL/g;

    Tested.