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

I'm having problems with writing to a file on Pentium 4 on 2000. I've seached for possible solutions but not successful. The code is below, can someone help, thanks.
my $sql_log = "c:\\temp\\sqllog.out"; open (FH, ">$sql_log") or die "can't open file $! \n"; while (<FH>) { print "test"; } close FH;
error message: Filehandle FH opened only for output at C:\Andre\Perl_scripts\timeout.pl line 26 .

Edit Masem 2001-11-27 - CODE tags

Replies are listed 'Best First'.
Re: Basic, writing to a file
by Masem (Monsignor) on Nov 27, 2001 at 19:39 UTC
    When you open your file with ">$sql_log", you're telling perl to open the file in write-only mode with the preceding > sign, which is probably what you want.

    However, when you do while (<FH>) you're telling perl to try to read from the file, which you've just told it it can't do, and thus you get your error. Instead, you probably want:

    my $sql_log = "c:\\temp\\sqllog.out"; open (FH, ">$sql_log") or die "can't open file $! \n"; print FH "test"; close FH;
    to simply write one line out to the file. Update yea, forgot the FH in the print line.

    If you're looking to do something more complex, let us know what you're trying to do as there's probably more to this code than what you've given us.

    -----------------------------------------------------
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
    "I can see my house from here!"
    It's not what you know, but knowing how to find it if you don't know that's important

      I think masem meant
      print FH "test";

                      - Ant
                      - Some of my best work - (1 2 3)

      Hey Masem, thanks for the information, I also found out why it was a problem before reading (while statement) your reply. dreman
Re: Basic, writing to a file
by IlyaM (Parson) on Nov 27, 2001 at 19:53 UTC
      Indeed, and make sure you understand the basics before you go meddling with SQL modules, because that will almost certainly involve objects and references, which fall outside the 'basic' category of things.