Apache-PerlVINC

 view release on metacpan or  search on metacpan

PerlVINC.pm  view on Meta::CPAN

package Apache::PerlVINC;

use strict;
use Apache::ModuleConfig ();
use Apache::Constants qw(DECLINE_CMD OK DECLINED);
use DynaLoader ();

$Apache::PerlVINC::VERSION = '0.03';


if($ENV{MOD_PERL}) 
{
  no strict;
  @ISA = qw(DynaLoader);
  __PACKAGE__->bootstrap($VERSION); #command table, etc.
}

sub new { return bless {}, shift }

#------------------------------------------------------------#
#---------------Configuration Directive Methods--------------#
#------------------------------------------------------------#

sub PerlINC ($$$) 
{
  my($cfg, $parms, $path) = @_;
  $cfg->{INC} = $path;
}

sub PerlVersion ($$@) 
{
  my($cfg, $parms, $name) = @_;
  $cfg->{'Files'}->{$name}++;
}



sub handler 
{
  my $r = shift;
  my $cfg = Apache::ModuleConfig->get($r, __PACKAGE__);

  if ($r->current_callback() eq "PerlCleanupHandler") 
  {
    map { delete $INC{$_} } keys %{$cfg->{Files}};
    return OK;
  }
   
  local @INC = @INC;
  unshift @INC, @{ $cfg->{'VINC'} };
  for (keys %{ $cfg->{'Files'} }) 
  {
    delete $INC{$_};
    #let mod_perl catch any error thrown here
    require $_;
  }
  
  return OK;
}

#------------------------------------------------------------#
#----------------Configuration Merging Routines--------------#
#------------------------------------------------------------#


sub DIR_CREATE
{
  my $self = shift->new();
  $self->{VINC} ||= [];
  $self->{Files} ||= {};
  return $self;
}


sub DIR_MERGE
{
  my ($prt, $kid) = @_;

  my %new = (%$prt, %$kid);

  $new{INC} = $prt->{INC} if $kid->{INC} eq "";
  %{$new{Files}} = (%{$prt->{Files}}, %{$kid->{Files}});

  # INC paths get built here
  @{$new{VINC}} = ($prt->{INC}, $kid->{INC});

  return bless \%new, ;
}


1;
__END__

=head1 NAME

  Apache::PerlVINC - Allows versioning of modules among directories or v-hosts.

=head1 SYNOPSIS

#example httpd.conf:


<VirtualHost dave.domain.com>

  # include the module. this line must be here.
  PerlModule Apache::PerlVINC

  # set the include path
  PerlINC /home/dave/site/files/modules
  # make sure VINC reloads the modules
  PerlFixupHandler Apache::PerlVINC
  # aptionally have VINC unload versioned modules
  PerlCleanupHandler Apache::PerlVINC



( run in 2.132 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )