T
Trotsky
Hi all,
Im trying to read and write to a text file using two scripts.
One to read the file into a text box, make changes then post it to
another script to actually overwrite the text file.
Ive cobbled together the scripts from various tutorials online, and as
you can see I have not got a clue what I am doing really.
Ive succeeded in making a script to delete a text file.
anyway heres the code for the READ.PL script
#!/usr/bin/Perl # This line applies to non-IIS users only
print "Content-type:text/html\n\n";
open(fileIN,"test.txt") or dienice("Cannot open test.txt: $!");
@logData = <fileIN>;
close(fileIN);
print <<EndHTML;
<html>\n<head>\n
<title> Log File Data </title>\n
</head>\n<body>\n
<h2>Log File Data</h2>\n
EndHTML
foreach $line (@logData)
{
chomp($line);
print "<form method=post action=write.pl><textarea cols=100 rows=20
name=text>$line<br>\n";
}
print "</textarea><input type=submit></form></body>\n</html>";
# Error Trapping Sub...should things go pear shaped!
sub dienice
{
my($msg) = @_;
print "<html>\n<head>\n<title>Error Opening File!</title>\n";
print "</head>\n";
print "<body><h2>Error</h2>\n<b>";
print $msg;
print "\n</b></body>\n</html>";
exit;
}
This code reads the text file perfectly (I think)
And heres the code for the WRITE.PL script
#!/usr/bin/perl
print "Content-type:text/html\n\n";
#makes data readable
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
foreach$pair (@pairs)
{
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$Form{$name} = $value;
}
#opens text file
open(fileOUT, ">test.txt") or dienice("Can't open log.txt for writing:
$!");
#WRITES FORM DATA TO TEXT FILE
print fileOUT "$value";
close(fileOUT);
# If all went well, output thank you message
print <<EndHTML;
<html><head><title>Thank You</title></head>
<body>
<h2>Thank You!</h2><br>
The following information has been logged:<br><br>
$line.<br>
</body></html>
EndHTML
Im running on IIS 5.1 locally with perl 5.6.1
Any help appreciated. Im a complete newbie so you may have to talk quite
slow..
Thanks
John
Im trying to read and write to a text file using two scripts.
One to read the file into a text box, make changes then post it to
another script to actually overwrite the text file.
Ive cobbled together the scripts from various tutorials online, and as
you can see I have not got a clue what I am doing really.
Ive succeeded in making a script to delete a text file.
anyway heres the code for the READ.PL script
#!/usr/bin/Perl # This line applies to non-IIS users only
print "Content-type:text/html\n\n";
open(fileIN,"test.txt") or dienice("Cannot open test.txt: $!");
@logData = <fileIN>;
close(fileIN);
print <<EndHTML;
<html>\n<head>\n
<title> Log File Data </title>\n
</head>\n<body>\n
<h2>Log File Data</h2>\n
EndHTML
foreach $line (@logData)
{
chomp($line);
print "<form method=post action=write.pl><textarea cols=100 rows=20
name=text>$line<br>\n";
}
print "</textarea><input type=submit></form></body>\n</html>";
# Error Trapping Sub...should things go pear shaped!
sub dienice
{
my($msg) = @_;
print "<html>\n<head>\n<title>Error Opening File!</title>\n";
print "</head>\n";
print "<body><h2>Error</h2>\n<b>";
print $msg;
print "\n</b></body>\n</html>";
exit;
}
This code reads the text file perfectly (I think)
And heres the code for the WRITE.PL script
#!/usr/bin/perl
print "Content-type:text/html\n\n";
#makes data readable
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
foreach$pair (@pairs)
{
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$Form{$name} = $value;
}
#opens text file
open(fileOUT, ">test.txt") or dienice("Can't open log.txt for writing:
$!");
#WRITES FORM DATA TO TEXT FILE
print fileOUT "$value";
close(fileOUT);
# If all went well, output thank you message
print <<EndHTML;
<html><head><title>Thank You</title></head>
<body>
<h2>Thank You!</h2><br>
The following information has been logged:<br><br>
$line.<br>
</body></html>
EndHTML
Im running on IIS 5.1 locally with perl 5.6.1
Any help appreciated. Im a complete newbie so you may have to talk quite
slow..
Thanks
John