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

I use the following code to try to print to a file:
open(TXT,">hi.txt") || die; print TXT "hi\n"; close(TXT);
or I do
open(TXT,">hi.txt") || die; select(TXT); print "hi\n"; close(TXT);
When I run this in command line it creates the file just fine. When I run this on the IIS server on my laptop, it does nothing. I wondered if it was that I didn't have my IIS server set up right, so I uploaded it to a remote server, and I get the same issue. Am I missing something? Thanks for any help.

Replies are listed 'Best First'.
Re: Printing to a file not working
by linuxer (Curate) on Jul 28, 2008 at 06:54 UTC

    If open fails, the system error is stored in $!. You should return that error message and check your logfiles.

    open my $handle, '>', $file or die "$file: open failed: $!"; print $handle "text\n" or die "$file: print faield: $!"; close $handle or die "$file: close failed: $!";

    If you haven't access to the server's logfiles, try using

    use CGI::Carp qw(fatalsToBrowser);

    That should send fatal error messages to the browser.

    Did you already check the permissions? Has the webserver permissions to create/overwrite that file?

    update: reformatted code

Re: Printing to a file not working
by psini (Deacon) on Jul 28, 2008 at 08:23 UTC

    I see you are using a filename without path, this means you are opening the file in the current directory. But while from command line you know which directory is current, when your script is running as cgi you can't know it, so you should always use fully qualified filenames and/or explicitly set the current directory with chdir.

    Rule One: "Do not act incautiously when confronting a little bald wrinkly smiling man."

      My thoughts too. There may be also permissions issues, like the user by which the webserver is running not having permissions to write the files.

        Yes, but it is difficult checking the write permission on a dir you don't know where is... :)

        Rule One: "Do not act incautiously when confronting a little bald wrinkly smiling man."

Re: Printing to a file not working
by whereiskurt (Friar) on Jul 28, 2008 at 11:28 UTC
    zuluwarhelmet,

    Everyone else has nailed it -- permission issue (probably.) Just thought I could throw one more idea at you:

    use strict; use warnings; use File::Temp qw/ tempfile tempdir /; my $dir = tempdir(); my ($fh, $filename) = tempfile( DIR => $dir ); print "Temporary file '$filename' created.\n"; print $fh "Stuff you want in the file.\n";

    That will pretty much guarantee the creation of the file. It creates a unique subfolder inside of the $temp directory and creates a unique filename to write to. This is useful with webapps because each subsequent request won't clobber the previous file.

    Kurt

Re: Printing to a file not working
by zuluwarhelmet (Novice) on Jul 28, 2008 at 17:14 UTC
    Thanks for all the help. Apparently taking a few years off from doing anything with Perl had me forget some of the smaller details.. I changed ">text.txt" to ">./text.txt" and that seemed to fix the problem