jason dancks
jason dancks

Reputation: 1142

perl: What would cause perl to not find a file that exists?

Running a perl cgi script I got the error it couldn't find the file it was trying to open. I check apache error log /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

double check:

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

Why would this happen? This is a debian machine running apache 2.2 IDK if that helps.

There are 2 files: submit.cgi and 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;
}

Data::Dumper:

$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

Upvotes: 0

Views: 156

Answers (2)

ysth
ysth

Reputation: 98378

As your hexdump shows, pwd returns the working directory followed by a newline, and that is what is you are assigning to $path. Then you try to open "/home/jddancks/Documents/perl/homeworks/hw13\n/grades4.txt" which indeed has a directory that does not exist.

Try doing:

chomp( my $path = `pwd` );

Upvotes: 4

tripleee
tripleee

Reputation: 189297

If Apache is configured to run in a chroot it does not see /home on the host system at all.

Upvotes: 1

Related Questions