tags:

views:

137

answers:

5

I'm trying to use the Tenjin module but it fails because it can't find the template file but it exists. I've added some debug statements into the module and it's not passing

return $filepath if (-f $filepath);

even when $filepath is correct. I've tried in a standalone script and it works fine but when I copy it to the mod_perl script it fails. Any ideas?

$filepath is a full absolute path: /something/another/dir/2/filename.plhtml

This is the function form the module. Notice my "Debug"...it prints the correct path to the file which is 777 but it never prints YES.

sub find_template_file {
my ($this, $filename) = @_;

my $path = $this->{path};
if ($path) {
    my $sep = $^O eq 'MSWin32' ? '\\\\' : '/';
    foreach my $dirname (@$path) {
        my $filepath = $dirname . $sep . $filename;
        print STDERR "--$filepath--\n";
        if (-f $filepath){
            print STDERR "--- YES ---\n\n";
        }
        return $filepath if (-f $filepath);
    }
} else {
    return $filename if (-f $filename);
}
my $s = $path ? ("['" . join("','", @$path) . "']") : '[]';
die "Tenjin::Engine: $filename not found (path=$s).";

}

Fails with

Tenjin::Engine: index.plhtml not found (path=['/var/2.0/templates/search']). at /usr/lib/perl5/site_perl/5.8.8/Tenjin/Engine.pm line 56.\n

A: 

Are you using absolute or relative pathnames? Your assumptions about the current directory may simply be wrong.

innaM
+1  A: 

Give -f the full path to the file, and make sure it is readable by Apache.

Brad Gilbert
+5  A: 

The Apache process also needs read and execute access on every subdirectory up to the full path. (If symbolic links are involved, it will be trickier to determine what the accesses are).

If you can debug the script in place on the web server, you might want to get Perl to deliver you an error message:

if (! -f $filename) {
    open(ACK, "<", $filename);
    print STDERR "Couldn't open $filename because of: $!\n";
}
mobrule
Only execute access to be precise.
Gleb
@Gleb - Good point!
mobrule
I feel stupid not thinking of that, particularly since I saw a question with the same problem on ServerFault earlier today.
R. Bemrose
A: 

I'm going to totally ignore what you asked and answer something completely different instead! I'm just that crazy!

Well, not really, I'm leveraging a core perl module, File::Find, instead of writing my own directory parser.

On request, here's the question I'm actually answering: "How do I find the path to a file that is somewhere in a sub-directory of a specific set of paths?"

use File::Find;

# Other parts of the class here

sub find_template_file {
    my ($this, $filename) = @_;

    my $file_path;

    my $path = $this->{path};

    # Note that this inner sub uses variables we defined above
    find(sub {
        if ($_ eq $filename)
            $file_path = $File::Find::name;
    }, @$path);

    if ($file_path)
        return $file_path;

    my $s = $path ? ("['" . join("','", @$path) . "']") : '[]';
    die "Tenjin::Engine: $filename not found (path=$s).";
}
R. Bemrose
Perhaps you could explicitly state the question you *are* answering?
ysth
ysth: I added it to my response, but I'll stick it here too: "How do I find the path to a file that is somewhere in a sub-directory of a specific set of paths?"
R. Bemrose
+2  A: 

-f will return false if the file doesn't exist but undef if the stat call failed for some other reason.

Test if the return is defined and if it is not, show the error that will have been set in $!. That may give you a clue.

ysth