user3518089
user3518089

Reputation: 47

Open filehandle not working under mod_perl ModPerl::PerlRun

I'm at my first attempt to use mod_perl. I'm totally new to it. I opted for ModPerl::PerlRun because I don't want to make any modification to the scripts I already have

I followed the instructions in Installing Apache2/Modperl on Ubuntu 12.04

I uploaded script.pl to /perl, and the script looks like it's running fine except for this

open(my $fh, '<:encoding(UTF-8)', 'page_template.htm') or die $!;

It won't open the file and dies with the message

No such file or directory at /var/www/perl/script.pl

Upvotes: 0

Views: 379

Answers (1)

Borodin
Borodin

Reputation: 126722

Update

Note that the documentation for ModPerl::PerlRun has this to say

META: document that for now we don't chdir() into the script's dir, because it affects the whole process under threads.

so it is probably not workable to simply do a chdir in your program's code, and the second option below should be used


Original*

The current working directory of your CGI program isn't what you think. It is most likely to tbe the root directory /

You can either use chdir to set the working directory of the script

use File::Basename 'dirname';
chdir dirname(__FILE__);

or simply add the full path to the name of the file that you want to open, for instance

open my $fh, '<:encoding(UTF-8)', '/perl/page_template.htm' or die $!;

Note that you can't use FindBin, as your program is being run as a subroutine of Apache's main mod_perl process, so $FindBin::Bin will be equal to the directory of the Apache executable httpd and not of your own program file

Upvotes: 1

Related Questions