httpd-apreq-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Steve Hay <steve....@uk.radan.com>
Subject Subclassing Apache::Request
Date Thu, 24 Apr 2003 08:36:11 GMT
Hi,

I'm trying to write a sub-class to Apache::Request and I find that I 
can't inherit its constructor because it always blesses the new object 
into Apache::Request itself rather than the invocant class, i.e. the 
program:

   package Foo;
   use Apache::Request;
   BEGIN {
       our @ISA = qw(Apache::Request);
   }

   package main;
   MAIN: {
       my $r = Apache->request();
       my $foo = Foo->new($r);
       print "Content-type: text/html\n\n";
       print "foo is-a ", ref($foo), "\n";
   }

prints "foo is-a Apache::Request" instead of "foo is-a Foo".

Is this intentional or a bug?  I posted this question yesterday to 
modperl@perl.apache.org, and Joe Schaefer suggested that it might be a 
necessary consequence of having Apache::Request::new() implemented in XS 
instead of Perl.  Is this the case, or could it be fixed?

I can, of course, write my own constructor in the sub-class to simply 
re-bless the new object:

   sub new {
       my($class, @args) = @_;
       return bless $class->SUPER::new(@args), $class;
   }

but it would be better not to have to do this unless there is a good 
reason for it.

The other idea that came back from the mod_perl mailing list was to 
aggregate:

    sub new {
        my($class, @args) = @_;
        return bless { _r => Apache::Request->new(@args) }, $class;
    }

but that would then require an AUTOLOAD() method or similar to delegate 
Apache::Request methods to the contained Apache::Request object; this is 
even more annoying than my "re-blessing" suggestion above.

Ideally I would like to have Apache::Request fixed so that it can be 
straightforwardly sub-classed.

If this is not possible (either due to the XS implementation or some 
reason) then what is the best way to effect the same behaviour?  Is it 
"safe" to re-bless the object, or must I aggregate & delegate?

Steve


Mime
View raw message