|
Problem with writing file in perl
Hi,
I am new to perl cgi. I am trying to implement a counter but i am not being able to open the file for writing.
Below is my perl code.
I have chmod 777 both on the directory as well as the text file I want to update.
Every Time I run the program I get Error - could not open file. What am I doing wrong?
Thanks in advance
a newbie
#!/usr/bin/perl
$log_file = "counter/data.txt";
$hitcount=0;
print "Content-type: text/html\n\n"; # Web page as default output
open (COUNT, $log_file) or &error("could not open file"); # Open for read using COUNT file handle
$hitcount = <COUNT>; # Read in file to get current count value
close COUNT; # Close the data file
print $hitcount;
$hitcount = $hitcount + 1; # Increment count value
open (OUT, ">$log_file") or &error("Could not open file"); # Open data file for write
flock(OUT, 2); # Locks file
print OUT "$hitcount"; # Write updated count value
close OUT; # Close the data file
exit;
sub error
{
my($errmsg) = @_;
print "<h2>Error</h2>\n";
print "<p>$errmsg</p>\n";
print end_html;
exit;
}
|