perl:什么会导致perl找不到存在的文件?

时间:2015-01-14 05:29:30

标签: perl

运行perl cgi脚本我收到错误,它无法找到它试图打开的文件。我检查了apache错误日志/var/log/apache2/error.log:

[Tue Jan 13 20:59:17 2015] [error] [client ::1] [Tue Jan 13 20:59:17 2015] submit.cgi: [Tue Jan 13 20:59:17 2015] submit.cgi: 
/home/jddancks/Documents/perl/homeworks/hw13/grades4.txt: No such file or directory at /var/www/homeworks/hw13/CreateExam.pm line 48., referer: http://localhost/homeworks/hw13/test.cgi

仔细检查:

root@debian-macbook:/var/log/apache2# ls -l /home/jddancks/Documents/perl/homeworks/hw13/grades4.txt
-rwxrwxrwx 1 jddancks jddancks 2095 Jan  7 12:25 /home/jddancks/Documents/perl/homeworks/hw13/grades4.txt

为什么会这样?这是一个运行apache 2.2 IDK的debian机器,如果有帮助的话。

有两个文件:submit.cgi和CreateExam.pm。 submit.cgi:

use CreateExam;
...
my $path = `pwd`;
...
my $check = CreateExam->new("${path}/exam4.txt","${path}/answers4.txt","${path}/grades4.txt",$pathroot);
$check->entergrades($cookie_value,$cgi->Vars());

CreateExam.pm:

package CreateExam;

sub new {
    my ($class,$file,$answers,$grades,$script) = @_;
    #print "<p>in new: file: $file, grades: $grades</p>\n";
    return bless {'file'=>$file,'answers'=>$answers,'gradefile'=>$grades,'script'=>$script},$class;
}

sub tooktest {
    my ($self,$person) = @_;
    #print "<p>in tooktest: person: $person</p>\n";
    my $grades = $self->{'gradefile'};
    open(ANS,"< $grades") or die "$grades: $!";
    my $found = 0;
    LAST: while(my $line = <ANS>) {
        if($line =~ /\<test taker=$person/) { $found = 1; last LAST;}
    }
    return $found==1;
}

数据::自卸车:

$VAR1 = "/home/jddancks/Documents/perl/homeworks/hw13"; (in browser)

hexdump都:

jddancks@debian-macbook:~/Documents/perl/homeworks/hw13$ perl -e 'print qx(pwd)' | hexdump -C
00000000  2f 68 6f 6d 65 2f 6a 64  64 61 6e 63 6b 73 2f 44  |/home/jddancks/D|
00000010  6f 63 75 6d 65 6e 74 73  2f 70 65 72 6c 2f 68 6f  |ocuments/perl/ho|
00000020  6d 65 77 6f 72 6b 73 2f  68 77 31 33 0a           |meworks/hw13.|
0000002d

2 个答案:

答案 0 :(得分:4)

正如你的hexdump所示,pwd返回工作目录后跟换行符,这就是你要分配给$ path的内容。然后你尝试打开"/home/jddancks/Documents/perl/homeworks/hw13\n/grades4.txt",它确实有一个不存在的目录。

尝试做:

chomp( my $path = `pwd` );

答案 1 :(得分:1)

如果Apache配置为在chroot中运行,则根本不会在主机系统上看到/home