mod_perl(2) abstraction

Beatnik on 2006-05-04T12:00:53

I'm writing a quick abstraction layer so I can use my app in both mod_perl and mod_perl 2. I'm not using any of the fancy bucket stuff in mod_perl 2 ofcourse. I don't want to depend on Apache2::Compat too much so I cooked up something really quick:

sub new {
 my $proto  = shift;
 my $class  = ref($proto) || $proto;
 my $self = {};
 $self->{REQUEST} = shift; #This would be Apache::Request or Apache2::Request
 if (ref($self->{REQUEST}) eq "Apache::Request")
 { eval qq|use Apache::Cookie; \$self->{COOKIES} = Apache::Cookie->fetch; |; 
  eval qq|use Apache::Request; \$self->{REMOTE_HOST} = \$self->{REQUEST}->get_remote_host(); |; 
  }
 if (ref($self->{REQUEST}) eq "Apache2::Request")
 { eval qq|use Apache2::Cookie; \$self->{COOKIES} = Apache2::Cookie->fetch; |; 
  eval qq|use Apache2::Request; \$self->{REMOTE_HOST} = \$self->{REQUEST}->connection->get_remote_host(); |; 
 }
 bless $self,$class;
 return $self;
} 


I've taken some ideas from a module on CPAN (guessing it was OpenInteract but I'm not sure anymore). For most part, the other Apache(2)::Request method names are the same (but I guess, time will tell if that's true if my app starts falling apart). Now all I need is a way to test changes in my main code for both platforms. I guess I'll have the same problem when I want to test my code on both mysql and postgresql. I'll cook something up in Mech probably.


`ref()` is suboptimal

Aristotle on 2006-05-04T19:43:45

Is there a reason why you don’t do $self->{REQUEST}->isa("Apache::Request")? In this case, it’s not likely to be a big deal, but there doesn’t seem to be any good reason not to do it, and you never know when you’ll need the flexibility.