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

I am writing some code that reads from a file, places the code in a form tag in my hmtl, and when I submit the form I would like to overwrite the file with data from the form. The only problem is is that I can only get a read and not a write command to work, and since my code is on a Windows box that I only have ftp access too I have no control over permissions. When I try to check the file's status with a '-w' it says that I have write permissions. Here is what I'm using:
my $filename= "$Bin/myfilename.cgi"; #filename #open file with read access and save to array -- this works open(TEST, "$filename") || &ErrorMessage("Can't read file"); @mycontentfromfile = <TEST>; close(TEST); my $guidelines = "content I would like to use to overwrite existing"; #open file with write access -- get error message listed below open(TEST, "> $filename") || &ErrorMessage("Can't open the file to sav +e content"); print TEST "$guidelines\n"; close(TEST ); #this print statement is executed if (-w $filename) { print "This file has write permissions". }
I have tried every different type of open and the only thing that works is the read permissions. Help please.

Replies are listed 'Best First'.
Re: Problems writing to an external file
by dws (Chancellor) on Nov 13, 2001 at 06:22 UTC
    When an open() fails, Perl puts an error message into the special variable $! (see perlvar for details).

    Try   open(TEST, ">$filename") or ErrorMessage("$filename: $!"); The contents of the error message will distinguish the type of failure.

Re: Problems writing to an external file
by Bobcat (Scribe) on Nov 13, 2001 at 07:23 UTC
    Looks like there's an extra space between the > and "$filename" in the open statement.

    Try removing that and see if it fixes it. Change: open(TEST, "> $filename") || &ErrorMessage( Can't open the file to save content"); to: open(TEST, ">$filename") || &ErrorMessage( Can't open the file to save content"); EDIT: tilly informed me that the space is, in fact, preferred style. Ooops.

      After struggling, it appears to be a directory permission problem. This begs the question, is "-w" broken on Win32 Perl?
Re: Problems writing to an external file
by Gerryjun (Scribe) on Nov 13, 2001 at 17:22 UTC
    If the problem is on file permission, you can actual change file & directory permission using FTP, on WS_FTP Pro you can right click on a file name to cmode and change its permission, but i assume its Unix. Im sure you FTP can change remote permission somewhere in its commands.
Re: Problems writing to an external file
by mce (Curate) on Nov 13, 2001 at 13:53 UTC
    Hi Willick,
    Just a thought,
    You are not trying to overwrite yourself, aren't you?
    What I mean is, do you want to overwrite the cgi script you ac cesses via the browser?

    You can dump the errors to the web browser using cpan:CGI::Carp, but I guess you already know this. Dr. Mark Ceulemans

    Senior Consultant

    IT Masters, Belgium

Re: Problems writing to an external file
by Purdy (Hermit) on Nov 13, 2001 at 18:52 UTC
    Trying to understand your purpose and I believe you may want to check out the HTML::Template module from CPAN. With it, you can create a "surround" (or template) file and pinpoint exactly where you want your $guidelines to go and then write the merged file where-ever you want. It looks like that's where you want to go since you're also reading in the filename, but not doing anything with it (yet). :)

    Code for the template ($Bin/myfilename.TMPL):

    Here are my guidelines: <TMPL_VAR NAME=GUIDELINES>

    Then here would be the code for your CGI script:

    # pre: $guidelines is defined, HTML::Template is installed # in your @INC and you have write privs to $filename use HTML::Template; my $template = HTML::Template->new ( filename => "$Bin/myfilename.TMPL" ); $template->param ( GUIDELINES => $guidelines ); # now you can either print the template to STDOUT or a filehandle # Option #1 (overwriting your .cgi file) open ( TEST, ">$filename" ); print TEST $template->output; close ( TEST ); # Option #2 (printing it to STDOUT - if to a Web browser, # make sure you've already sent the Content-type ahead of # this [next line]). print "Content-type: text/html\n\n"; print $template->output;

    My apologies if I read this the wrong way - I just implemented HTML::Template myself and am enjoying it a lot. You will still need to work around your write privs issue if you want to write to a file (the previous responses will help you with that).

    Jason