Hammy has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I have a simple task that I want to do and I am having the worst trouble. All I want to do is open an html file using perl. My code is as follows:
#!/usr/local/bin/perl use DBI; use CGI; print "Content-type: text/html\n\n"; use CGI::Carp qw(fatalsToBrowser carpout); print "opening"; open( HTML, "<../index.htm" ) or die( "Cannot access HTML index.htm"); print "opened";

I am using this to pass parameters to HTML files. Embedded in the HTML files are variables set up like -myvariable-. After opening the file, reading in all the lines, I replace the -myvariable- with a parameters passed to the CGI script, and print out the page. This works like a charms on a couple of my shared hosting site and it does not work here (a Virtual Private Server where I have full control of my portion of the server). I'm wondering if I have to set Apache parameters that allows me to open files. Thank you for whatever wisdom you can send my way.

Replies are listed 'Best First'.
Re: Confusion opening a file
by bunnyman (Hermit) on Oct 01, 2003 at 16:01 UTC

    Just two things:

    • Don't bother putting the use CGI::Carp inside the code. It will execute before the print statements do, regardless of where you put that line.
    • Display the cause of the error by using $! in your die message, like this: die( "Cannot access index.htm: $!" );

    Most likely the file permissions do not allow the script to open the file. Remember that you might have more permissions than the script has, if the script is not running as you.

      My Apache runs as user "nobody", so whatever user your Apache runs as will need "r"ead access to the file you're trying to open. I'M NOT CERTAIN about this, but I think that if you want to grant the webserver(cgi script) read access to a file, and the absolute path to the file is
      /path/to/read_file.html
      then
      directory 'path' needs to grant read and execute privs to the webserver user, and directory 'to' needs to grant read and execute privs to the webserver user, and file read_file.html needs to grant read privs to the webserver user.
      I think(?) the directories need to grant 'execute' privs to enable descending into those directories. On my system I tried this with 'root' owner and 'root' group owning all files and directorys under /path - so to allow webserver user "nobody" to be able to read /path/to/read_file.html, I had to do
      chmod o+rx /path chmod o+rx /path/to chmod o+r /path/to/read_file.html
      HTH.
      Thanks for the advice. Through a lot of trial and error I found a solution. I was trying to open the file with a relative address, but the official domain is not yet pointing to the machine (so it could not find it). When I use $ENV{DOCUMENT_ROOT} it finds the file (my problem is it was not finding it - I was reading an old log file that said the error was permission related. As you can tell, I am not real strong with administration issues, but I am getting there.
Re: Confusion opening a file
by menolly (Hermit) on Oct 01, 2003 at 16:54 UTC
    Check the permissions, as per the previous monks' advice, and then check out HTML::Template, since it sounds like you're rolling your own templating system.