You are viewing a plain text version of this content. The canonical link for it is here.
Posted to docs-cvs@perl.apache.org by st...@apache.org on 2003/07/25 11:27:10 UTC

cvs commit: modperl-docs/src/docs/2.0/user/porting compat.pod porting.pod

stas        2003/07/25 02:27:10

  Modified:    src/docs/2.0/user/porting compat.pod porting.pod
  Log:
  add examples on how to have : method and ($$) coexist
  
  Revision  Changes    Path
  1.14      +5 -0      modperl-docs/src/docs/2.0/user/porting/compat.pod
  
  Index: compat.pod
  ===================================================================
  RCS file: /home/cvs/modperl-docs/src/docs/2.0/user/porting/compat.pod,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- compat.pod	25 Jul 2003 09:08:03 -0000	1.13
  +++ compat.pod	25 Jul 2003 09:27:10 -0000	1.14
  @@ -1146,6 +1146,11 @@
   If C<Class-E<gt>method> syntax is used for a C<Perl*Handler>, the
   C<:method> attribute is not required.
   
  +The porting tutorial provides
  +L<examples|docs::2.0::user::porting::porting/Method_Handlers> on how
  +to use the same code base under both mod_perl generations when the
  +handler has to be a method.
  +
   
   =head1 C<Apache::src>
   
  
  
  
  1.10      +144 -0    modperl-docs/src/docs/2.0/user/porting/porting.pod
  
  Index: porting.pod
  ===================================================================
  RCS file: /home/cvs/modperl-docs/src/docs/2.0/user/porting/porting.pod,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- porting.pod	18 Jun 2003 01:55:05 -0000	1.9
  +++ porting.pod	25 Jul 2003 09:27:10 -0000	1.10
  @@ -1300,6 +1300,150 @@
   
   =back
   
  +=head2 Method Handlers
  +
  +Method handlers in mod_perl are declared L<using the I<'method'>
  +attribute|docs::2.0::user::porting::compat/Method_Handlers>. However 
  +if you want to have the same code base for mod_perl 1.0 and 2.0
  +applications, whose handler has to be a method, you will need to do
  +the following trick:
  +
  +  sub handler_mp1 ($$)     { ... }
  +  sub handler_mp2 : method { ... }
  +  *handler = MP2 ? \&handler_mp2 : \&handler_mp1;
  +
  +Here are two complete examples. The first example implements
  +C<MyApache::Method> which has a single method that works for both
  +mod_perl generations:
  +
  +The configuration:
  +
  +  PerlModule MyApache::Method
  +  <Location /method>
  +      SetHandler perl-script
  +      PerlHandler MyApache::Method->handler
  +  </Location>
  +
  +The code:
  +
  +  #file:MyApache/Method.pm
  +  package MyApache::Method;
  +  
  +  # PerlModule MyApache::Method
  +  # <Location /method>
  +  #      SetHandler perl-script
  +  #      PerlHandler MyApache::Method->handler
  +  #  </Location>
  +  
  +  use strict;
  +  use warnings;
  +  
  +  use mod_perl;
  +  use constant MP2 => $mod_perl::VERSION < 1.99 ? 0 : 1;
  +  
  +  BEGIN {
  +      if (MP2) {
  +          require Apache::RequestRec;
  +          require Apache::RequestIO;
  +          require Apache::Const;
  +          Apache::Const->import(-compile => 'OK');
  +      }
  +      else {
  +          require Apache;
  +          require Apache::Constants;
  +          Apache::Constants->import('OK');
  +      }
  +  }
  +  
  +  sub handler_mp1 ($$)     { &run }
  +  sub handler_mp2 : method { &run }
  +  *handler = MP2 ? \&handler_mp2 : \&handler_mp1;
  +  
  +  sub run {
  +     my($class, $r) = @_;
  +     MP2 ? $r->content_type('text/plain')
  +         : $r->send_http_header('text/plain');
  +     print "$class was called\n";
  +     return MP2 ? Apache::OK : Apache::Constants::OK;
  +  }
  +
  +Here are two complete examples. The second example implements
  +C<MyApache::Method2>, which is very similar to C<MyApache::Method>,
  +but uses separate methods for mod_perl 1.0 and 2.0 servers.
  +
  +The configuration is the same:
  +
  +  PerlModule MyApache::Method2
  +  <Location /method2>
  +      SetHandler perl-script
  +      PerlHandler MyApache::Method2->handler
  +  </Location>
  +
  +The code:
  +
  +  #file:MyApache/Method2.pm
  +  package MyApache::Method2;
  +  
  +  # PerlModule MyApache::Method
  +  # <Location /method>
  +  #      SetHandler perl-script
  +  #      PerlHandler MyApache::Method->handler
  +  #  </Location>
  +  
  +  use strict;
  +  use warnings;
  +  
  +  use mod_perl;
  +  use constant MP2 => $mod_perl::VERSION < 1.99 ? 0 : 1;
  +  
  +  BEGIN {
  +      warn "running $mod_perl::VERSION!\n";
  +      if (MP2) {
  +          require Apache::RequestRec;
  +          require Apache::RequestIO;
  +          require Apache::Const;
  +          Apache::Const->import(-compile => 'OK');
  +      }
  +      else {
  +          require Apache;
  +          require Apache::Constants;
  +          Apache::Constants->import('OK');
  +      }
  +  }
  +  
  +  sub handler_mp1 ($$)     { &mp1 }
  +  sub handler_mp2 : method { &mp2 }
  +  
  +  *handler = MP2 ? \&handler_mp2 : \&handler_mp1;
  +  
  +  sub mp1 {
  +     my($class, $r) = @_;
  +     $r->send_http_header('text/plain');
  +     $r->print("mp1: $class was called\n");
  +     return Apache::Constants::OK();
  +  }
  +  
  +  sub mp2 {
  +      my($class, $r) = @_;
  +      $r->content_type('text/plain');
  +      $r->print("mp2: $class was called\n");
  +      return Apache::OK();
  +  }
  +
  +Assuming that mod_perl 1.0 is listening on port 8001 and mod_perl 2.0
  +on 8002, we get the following results:
  +
  +  % lynx --source http://localhost:8001/method
  +  MyApache::Method was called
  +
  +  % lynx --source http://localhost:8001/method2
  +  mp1: MyApache::Method2 was called
  +
  +  % lynx --source http://localhost:8002/method
  +  MyApache::Method was called
  +
  +  % lynx --source http://localhost:8002/method2
  +  mp2: MyApache::Method2 was called
   
   
   
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: docs-cvs-unsubscribe@perl.apache.org
For additional commands, e-mail: docs-cvs-help@perl.apache.org