package Apache::Forward;

use strict;
use Apache::Constants qw(OK SERVER_ERROR);
use vars qw($VERSION);
$VERSION = '1.00';

sub handler ($$) {
    my($class, $r) = @_;
    my $next = tied *STDOUT || return SERVER_ERROR;
    tie *STDOUT, $class, $r, $next or return SERVER_ERROR;
    $r->register_cleanup(sub { untie *STDOUT });
    OK;
}

sub TIEHANDLE {
    my($class, $r, $next) = @_;
    bless { 'r' => $r, 'next' => $next}, $class;
}

sub PRINT {
    my $self = shift;
    # Subclasses should do something interesting here
    $self->forward(@_);
}

#sub DESTROY {
#    my $self = shift;
#    # maybe clean up here
#}

sub forward {
    shift()->{'next'}->PRINT(@_);
}

1;
__END__
