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

I have a question about using OPEN. On a friend of mine's Apache web server, I have the following bit of code:
open(PTR,">>nate.txt");
This opens that log file.

On another web server (also, Apache), I had the same thing, same exact program and everything. It wouldn't work. All I would get is this annoying: 500 Internal Server Error. In the log file, it would give me a Premature end of script headers error: failed to open log file and fopen: Permission denied.
I had the permissions correctly set to read and write on that "nate.txt" file. What I did to remedy that error, was to change that OPEN line to:

open(PTR, '>>nate.txt');
My question is, why does it matter if I use the single-quote or the double-quotes? Why doesn't it work with the double-quotes? I'd appreciate any help. Thanks!
Nate

Replies are listed 'Best First'.
(tye)Re: Question about OPEN
by tye (Sage) on Dec 05, 2001 at 20:21 UTC

    My first guess is that your current working directory isn't where you thought it was. You shouldn't rely on your script being run with any specific working directory so opening a file using anything other than a full, absolute path is likely to cause you problems. Either chdir to the desired directory early in the script or only use full, absolute paths.

            - tye (but my friends call me "Tye")
Re: Question about OPEN
by Beatnik (Parson) on Dec 05, 2001 at 19:38 UTC
    The >filename is open for appending (perlfunc::open & perlopentut). If the file does not exist, it'll be created (and you need directory permissions for that - ofcourse you also need them to append anyway). Check if the user nobody can access the file you wish to open.
    I suggest you use some I/O checking and use
    open(FILE,">>filename") || die "Oops : $!";
    The double/single quote is all about interpolation. Special values (like variables, tabs & newlines) are parsed and then printed with double quotes. Single quotes force characters into lexicallity :) If you use a \n in single quotes, no newline is printed but the string \n. See perlop for more on interpolation.

    Greetz
    Beatnik
    ... Quidquid perl dictum sit, altum viditur.
Re: Question about OPEN
by strat (Canon) on Dec 05, 2001 at 19:32 UTC
    Maybe the following construct gives you better information:
    unless ( open(PTR, '>>nate.txt') ){ print "Error: couldn't open nate.txt: $!"; } else { # rest of code here... }