Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
Just another Perl shrine
 
PerlMonks  

Can't open file using Perl as CGI

by timtowtdi (Acolyte)
on May 19, 2008 at 21:40 UTC ( #687472=perlquestion: print w/ replies, xml ) Need Help??
timtowtdi has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I am struggling with opening files using Perl as CGI.
I just want to show the contents of a file (just in the testing fase)
I use this simple code:
#!/usr/bin/perl

use strict;

print "Content-type: text/plain\n\n";

open (INFILE, '<', 'textfile.txt');
while (<INFILE>)
{
  print "$_\n";
}
close (INFILE);
The trouble is: I don't get an error-message, but I also don't see the contents of textfile.txt. If I try to print other stuff, for example: I add the line: print "Hello\n"; under the line where I close INFILE, it will return the string Hello in my browser.
Even if the file textfile.txt doesn't exist, it won't return an error-message. Not in my browser and not in my logfile on my server. If I make other mistakes, for example, something with the syntax, it will return error-messages in both my browser and the error-logs of Apache.

Any of you Monks an idea? :) Thanx in advance,

Tim

Server running: Gentoo Linux with Apache 2.2.8 with mod_perl/2.0.3-dev Perl/v5.8.8

The great mistake is to anticipate the outcome of the engagement; Let nature take it's course, and your tools will strike at the right moment.

Comment on Can't open file using Perl as CGI
Re: Can't open file using Perl as CGI
by roboticus (Canon) on May 19, 2008 at 21:45 UTC
    timtowtdi:

    The program is likely (a) not running in the directory you think it is, and/or (b) not running as a user with permissions to read the file.

    You might try printing the current directory out in your program.

    ...roboticus

    who isn't going to even mention using the CGI.pm module....

      Thank you!

      I am embarassed to say how long I tried to figure out why the heck Perl could not find a file in the same directory as my script. I wasn't executing the script from the script's directory! Was typing in the whole path to the script and everything. duh! I'm fairly new, but even I know better than that. Thank you for pointing out the (much needed) obvious.

      If you were in the same room I would hug you.

      Seriously.

Re: Can't open file using Perl as CGI
by pc88mxer (Vicar) on May 19, 2008 at 21:46 UTC
    It's likely there's an error, but you have to check for it:
    open(INFILE, '<', 'textfile.txt') or die "unable to open textfile.txt: + $!"; ...
    and this will show the error in your server's error log.

    My guess is that you need to specify an absolute path for textfile.txt.

      That was it! The "or die"-addition!
      I already checked whether the script was running in the correct directory, and it did. It turned out to be quite easy: The rights set on 'textfile' weren't right; Apache wasn't able to read.
      Thanks Monks! You really helped me out!

      The great mistake is to anticipate the outcome of the engagement; Let nature take it's course, and your tools will strike at the right moment.
Re: Can't open file using Perl as CGI
by kyle (Abbot) on May 19, 2008 at 21:48 UTC

    If open has an error, you need to check for that explicitly.

    open INFILE, '<', 'textfile.txt' or print "Error: Can't read textfile.txt: $!";

    Normally that "print" would be a "die," but that would only show in your error log, not in your browser.

    My guess is that your CGI is not running in the directory you think it is. Try using a full path to the file you want to open. In any case, showing the error will help a lot.

Re: Can't open file using Perl as CGI
by jonnyfolk (Vicar) on May 20, 2008 at 06:56 UTC
    By the way, I note that you are adding a line break to your print statement without using chomp; This will give you more line breaks than you (possibly) intended...
    open (INFILE, '<', 'textfile.txt'); while (<INFILE>) { chomp; print "$_\n"; } close (INFILE);
      True!
      This "extra line break" was a result/remainder of some troubleshooting earlier that evening. :)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://687472]
Approved by pc88mxer
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (9)
As of 2014-04-20 00:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (485 votes), past polls