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

I'm new to perl, but this question possibly has more to do with what I may need to find out from the tech support for the service for our churches webpage. I've been trying to write a script to test that will write a simple text file to the server.

I have been using this script (pasted below) , thier perl is enabled because it prints "Hello world" to my browser window.

It just never writes a file and I get no errors in the browser.

I talked to the tech support there and they are running MS IIS 6.0 server software and has told me he has given permission for the script to read and write. It doesn't seem like they know much about scripting there.

I am not in control of who our church uses for server space so I'd appreciate any help in what I can possibly do or ask of that service to get a script to write a file.
Thanks This is the script.
#!/usr/local/bin/perl print "Content-Type: text/html\n\n"; print "<html> <head>\n"; print "<title>Hello, world!</title>"; print "</head>\n"; print "<body>\n"; print "<h1>Hello, world!</h1>\n"; print "</body> </html>\n"; $append = 0; if ($append) { open(MYOUTFILE, ">filename.out"); } else { open(MYOUTFILE, ">>filename.out"); #open for write, append } print MYOUTFILE "Testing 1 2 3"; #write text, no newline print MYOUTFILE "\n"; #write newline close(MYOUTFILE);

Replies are listed 'Best First'.
Re: Perl text file writing
by samtregar (Abbot) on May 25, 2006 at 20:38 UTC
    Where are you looking for the file? Maybe you need to specify the full path? I have no idea what IIS uses as a default cwd.

    Also, you really should be checking the return value on those opens:

      open(...) or die "Unable to open: $!";

    That will save you a lot of head-scratching later, although it's probably not going to help you right now since it seems the script is running to completion.

    -sam

Re: Perl text file writing
by VSarkiss (Monsignor) on May 25, 2006 at 20:39 UTC

    ... has told me he has given permission for the script to read and write.
    The follow-up question to that is, "Which directory does the script have permission to read and write?"

    When you try to open filename.out with no further qualifications, you're going to be writing in the directory the CGI program is running from. It's unlikely your script will have permission to do that.

    Try asking where your "upload directory" is. You should be able to write to that one.

Re: Perl text file writing
by duckyd (Hermit) on May 26, 2006 at 04:44 UTC
    As a side note, it looks like your append logic is backwards. I think you want
    if ($append) { open(MYOUTFILE, ">>filename.out"); # open for append } else { open(MYOUTFILE, ">filename.out"); #open for (over)write }
Re: Perl text file writing
by sithsasquatch (Scribe) on May 25, 2006 at 20:49 UTC
    you'll want to add the following beneath #!/usr/local/bin/perl
    use strict; use warnings; use diagnostics;
    The above lines will make Perl tell you what is going on when your script runs.
    Also look into the CGI::Carp module. That will direct any errors to your browser window.

      I don't think that's helpful. Per my reading of the program, adding these lines will produce no significant extra output.

      Why not tell the querant how to test for errors?

        Good point.

        A simple way to see if your script finished running is to add a few more print statements before your closing html tags, like so:
        print MYOUTFILE "Testing 1 2 3"; #write text, no newline print MYOUTFILE "\n"; #write newline print "Wrote to file \n"; #if this doesn't show in the browser, #the script didn't write to the file close(MYOUTFILE); print "Closed MYFILE \n"; #if this doesn't show in the browser, #the script didn't close the file print "</body> </html>\n";
        In general, tachyon's CGI Help Guide has been invaluable when I was figuring out CGI.

        Update: Forgot to do ID tags.