-f 文件测试运算符在 mod_perl 中工作吗?

发布于 2024-08-05 22:21:13 字数 1090 浏览 7 评论 0原文

我正在尝试使用 Tenjin 模块,但它失败了,因为它找不到模板文件,但它存在。我已经在模块中添加了一些调试语句,

return $filepath if (-f $filepath);

即使 $filepath 正确,它也不会通过。我已经在独立脚本中尝试过,它工作正常,但是当我将其复制到 mod_perl 脚本时,它失败了。有什么想法吗?

$filepath 是完整的绝对路径: /something/another/dir/2/filename.plhtml

这是模块的函数。注意我的“调试”...它打印了文件的正确路径,即 777,但它从不打印“是”。

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).";

Tenjin::Engine 失败

找不到 index.plhtml (path=['/var/2.0/templates/search'])。位于 /usr/lib/perl5/site_perl/5.8.8/Tenjin/Engine.pm 第 56 行。\n

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦里兽 2024-08-12 22:21:13

Apache 进程还需要对每个子目录直至完整路径的读取和执行访问权限。 (如果涉及符号链接,则确定访问内容将更加棘手)。

如果您可以在 Web 服务器上就地调试脚本,您可能希望 Perl 向您发送一条错误消息:

if (! -f $filename) {
    open(ACK, "<", $filename);
    print STDERR "Couldn't open $filename because of: $!\n";
}

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";
}
蓝海似她心 2024-08-12 22:21:13

-f 如果文件不存在则返回 false,但如果 stat 调用因其他原因失败则返回 undef。

测试返回是否已定义,如果未定义,则显示将在 $! 中设置的错误。
这可能会给你一个线索。

-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.

世界和平 2024-08-12 22:21:13

-f 提供文件的完整路径,并确保 Apache 可以读取该文件。

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

宛菡 2024-08-12 22:21:13

您使用的是绝对路径名还是相对路径名?您对当前目录的假设可能根本就是错误的。

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

小巷里的女流氓 2024-08-12 22:21:13

我将完全忽略您的问题并回答完全不同的问题!我就是这么疯狂!

好吧,不是真的,我正在利用核心 Perl 模块, File::Find ,而不是编写我自己的目录解析器。

根据要求,这是我实际回答的问题:
“如何找到位于一组特定路径的子目录中某个文件的路径?”

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).";
}

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).";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文