Forum Moderators: coopster & phranque

Message Too Old, No Replies

Trouble with writing to file using Perl

         

dreaming of nascar

3:34 am on Mar 30, 2003 (gmt 0)

10+ Year Member



I am trying to write some data to a file using Perl. I have done this before many times on this server and now for some reason it won't let me write to the file. I can read from the file and do everything else with my scripts except for writing to the file (both writing over and append does not work). I didn't know if somebody here would be able to help or not. A sample code of what I have coded in my script is below. Any help would be appreciated. Thanks

open (FILE, "test.txt");
@file = <FILE>;
close (FILE);
$variable = @file[0]++;

-- The part above works, part below doesn't

open (FILE2, ">test.txt");
print FILE2 "$variable";
close(FILE2);

D O N

ShawnR

3:55 am on Mar 30, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Don

You could try separating the '>' from the filename, as per the following syntax:

open OUTPUT_FILE, '>', "text.txt" or LogErrorAndDie("Couldn't open output file: text.txt", __FILE__, __LINE__);

print OUTPUT_FILE "Some text";

with the function LogErrorAndDie defined somewhere as follows:

sub LogErrorAndDie
{
print "ERROR: File: $_[1], Line: $_[2]\n";
print "----> $_[0]\n";
exit 1;
}

If that doesn't work, I'd suggest checking you have the appropriate permissions on the file or directory you are trying to write to.

ShawnR

8:05 am on Mar 30, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry, DON. On second thoughts, while

open HANDLE, '>', "filename"

works for me, your original syntax of

open HANDLE, ">filename"

is perfectly valid too.

Perhaps the problem is with what you are trying to print to the file. What is $variable set to before the print statement?

What are you trying to do with the statement:

$variable = @file[0]++;

$variable = $array[n]; sets variable to the nth element of the array
$variable = @array; sets variable to the size of the array
Not sure if $variable = @array[n]; is a reasonable thing to do.

Fischerlaender

11:52 am on Mar 30, 2003 (gmt 0)

10+ Year Member



Did you check if your file has the appropriate rights? Try to do "chmod 777 test.txt" before executing the script.

And put '.. or die "Could not open test.txt'" behind your open statement. So you can see if the error occurs opening the file or when trying to write to it.

andreasfriedrich

11:57 am on Mar 30, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




$variable = @array[n]++;

should be written as


$variable = $array[n]++;

as Perl [perl.com] will tell you when it is run with warnings enabled (-w switch or use [perldoc.com] warnings [perldoc.com]).

But otherwise it is perfectly legal. It will assign the old value of $array[n] to $variable and then increase it by one.

Andreas

andreasfriedrich

12:01 pm on Mar 30, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




>>And put '.. or die "Could not open test.txt'" behind your
>>open statement. So you can see if the error occurs
>>opening the file or when trying to write to it.

Even better yet put '.. or die "Could not open test.txt: $!" behind your opening statement. $! will contain the error message of what went wrong when Perl [perl.com] tried to open the file.

Andreas