Perrin Harkins <[EMAIL PROTECTED]> said something to this effect on 10/31/2001:
> > This is not the case.  INCLUDE_PATH is static and global.
> 
> You can change INCLUDE_PATH as often as you like.  Admittedly,
> the docs don't make this very clear, but it is an Officially
> Approved Technique.  I change it once per request, but as I
> suggested in my last mail you could change it in your
> Context::visit() method.
> 
> Here's an example (untested) that will make TT do a depth-first
> search from the current working directory:
> 
> use File::Spec;
> sub visit {
>     my ($self, $doc) = @_;
>     my $epath = $doc->{_epath};
>     my @inc_path;
> 
>     # this is from Darren Chamberlain's mail, with File::Spec to suport Win32
>     for (File::Spec->splitdir($epath)) {
>         push @inc_path, File::Spec->canonpath("$inc_path[-1]/$_");
>     }
>     @inc_path = reverse @inc_path;
> 
>     # set new INCLUDE_PATH
>     # the [0] will have to change if you use multiple Providers
>     $self->load_templates->[0]->include_path(\@inc_path)
> 
>     $self->SUPER::visit($self, $doc);
> }

It was pointed out to me that you can do:

  unshift @inc_path, File::Spec->canonpath("$inc_path[0]/$_");

instead of 

  push @inc_path, File::Spec->canonpath("$inc_path[-1]/$_");

and save the call to reverse on the return.

The main caveats with this are 1) that "/" is hardcoded into the
call to canonpath call and 2) File::Spec->splitdir doesn't exist
in the version that ships with perl 5.00503.

If you replace the use of push/reverse with unshift, you can
replace "$inc_path[-1]/$_" with File::Spec->catfile($inc_path[0], $_), 
which fixes caveat (1) above.  catfile gives an eror if pass
$inc_path[-1].

revised subroutine:

use File::Spec;
sub visit {
    my ($self, $doc) = @_;
    my $epath = $doc->{_epath};
    my @inc_path;

    # this is from Darren Chamberlain's mail, with File::Spec to suport Win32
    for (File::Spec->splitdir($epath)) {
        push @inc_path, File::Spec->canonpath(File::Spec->catfile($inc_path[0], $_))
    }
    @inc_path = reverse @inc_path;

    # set new INCLUDE_PATH
    # Multiple providers:
    for my $prov (@{$self->load_templates}) {
        $prov->include_path(\@inc_path)
    }

    $self->SUPER::visit($self, $doc);
}


(darren)

-- 
I am at two with nature.
    -- Woody Allen


Reply via email to