perl -c fails with FilterSnoop example in docs

perl -c fails with FilterSnoop example in docs

am 22.02.2011 21:32:18 von Mark Hedges

I copied this useful debugging filter at
http://perl.apache.org/docs/2.0/user/handlers/filters.html#A ll_in_One_Filter

It works fine, but I can't `perl -c` :

hedges@vm5:~/foo$ perl -c Lame.pm
Invalid CODE attribute: FilterConnectionHandler at Lame.pm line 26
BEGIN failed--compilation aborted at Lame.pm line 26.

If I comment those subs out it checks fine, so
Apache2::Filter is loading. It doesn't help if I also do
"use Apache2::Filter ()" first. Any ideas how to make this
check okay for my build test to include it? --mark--

#file:MyApache2/FilterSnoop.pm
#----------------------------
package Lame;

=head1 SEE ALSO

Search for L in the mod_perl2 pages at
http://perl.apache.org/.

=head1 METHODS

=cut

use strict;
use warnings;

use base qw(Apache2::Filter);
use Apache2::FilterRec ();
use APR::Brigade ();
use APR::Bucket ();
use APR::BucketType ();

use Apache2::Const -compile => qw(OK DECLINED);
use APR::Const -compile => ':common';

sub connection : FilterConnectionHandler { snoop("connection", @_) }
sub request : FilterRequestHandler { snoop("request", @_) }

=head2 snoop

snoop the buckets

=cut

sub snoop {
my $type = shift;
my ($f, $bb, $mode, $block, $readbytes) = @_; # filter args

# $mode, $block, $readbytes are passed only for input filters
my $stream = defined $mode ? "input" : "output";

# read the data and pass-through the bucket brigades unchanged
if (defined $mode) {
# input filter
my $rv = $f->next->get_brigade($bb, $mode, $block, $readbytes);
return $rv unless $rv == APR::Const::SUCCESS;
bb_dump($type, $stream, $bb);
}
else {
# output filter
bb_dump($type, $stream, $bb);
my $rv = $f->next->pass_brigade($bb);
return $rv unless $rv == APR::Const::SUCCESS;
}

return Apache2::Const::OK;
}

=head2 bb_dump

dump the buckets

=cut

sub bb_dump {
my ($type, $stream, $bb) = @_;

my @data;
for (my $b = $bb->first; $b; $b = $bb->next($b)) {
$b->read(my $bdata);
push @data, $b->type->name, $bdata;
}

# send the sniffed info to STDERR so not to interfere with normal
# output
my $direction = $stream eq 'output' ? ">>>" : "<<<";
print STDERR "\n$direction $type $stream filter\n";

my $c = 1;
while (my ($btype, $data) = splice @data, 0, 2) {
print STDERR " o bucket $c: $btype\n";
print STDERR "[$data]\n";
$c++;
}
}
1;

Re: perl -c fails with FilterSnoop example in docs

am 23.02.2011 00:34:51 von Randolf Richardson

> I copied this useful debugging filter at
> http://perl.apache.org/docs/2.0/user/handlers/filters.html#A ll_in_One_Filter
>
> It works fine, but I can't `perl -c` :
>
> hedges@vm5:~/foo$ perl -c Lame.pm
> Invalid CODE attribute: FilterConnectionHandler at Lame.pm line 26
> BEGIN failed--compilation aborted at Lame.pm line 26.
[sNip]
> sub connection : FilterConnectionHandler { snoop("connection", @_) }
> sub request : FilterRequestHandler { snoop("request", @_) }
[sNip]

Perhaps this will be of interest to you -- it mentions the need for
a handler subroutine:

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

The above link is part of the following discussion:

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

I hope that helps.

Randolf Richardson - randolf@inter-corporate.com
Inter-Corporate Computer & Network Services, Inc.
Vancouver, British Columbia, Canada
http://www.inter-corporate.com

Re: perl -c fails with FilterSnoop example in docs

am 23.02.2011 15:29:49 von Perrin Harkins

On Tue, Feb 22, 2011 at 3:32 PM, Mark Hedges wrote:
> I copied this useful debugging filter at
> http://perl.apache.org/docs/2.0/user/handlers/filters.html#A ll_in_One_Filter
>
> It works fine, but I can't `perl -c` :

Yes, the modules that are built to run in the context of Apache HTTPD
mostly will not run from the command-line. They are missing the
environment they expect. To test them, you need to run them through
mod_perl. See Apache::Test.

- Perrin