reading and writing to text file via form newbie

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
 
G

Gunnar Hjalmarsson

Trotsky said:
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.
Okay.

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.

That's a bad approach. Really bad. Never use such code unless you
don't understand what it does!!

You'd better learn enough to understand what the code you have
"cobbled together" does. That way you'll undoubtedly get input to fix
some of the bugs.
Before having done so, it's rude to post the code here.

An even better approach, of course, is to read some book - paper or on
line - about Perl and CGI, and learn some of the basics before
starting to write your own code that accomplishes the above task.
Any help appreciated.

Believe it or not, but my intent with the above is to help.
 
G

Gunnar Hjalmarsson

M said:
My thoughts exactly...!?!!
:)

Not _exactly_, I hope... (should of course have been "unless you
understand" or "if you don't understand"). ;-)
 
T

Trotsky

Didnt know about the no posting faux pas. Sorry
Its Just that I decided what I needed to do, and am trying to figure out
how to do it.
I find the best way is to Jump in at the deep end. Sink or Swim. And I
like to learn as I am doing. The Tutorials online are either the 'Hello
World' type or extremely complex, nothing in between. But ill keep
plugging away until I can do it.
If you want to reccommend a tutorial please feel free to.
Cheers John
 
G

Gunnar Hjalmarsson

Trotsky said:
Didnt know about the no posting faux pas. Sorry

Well, that was my reaction to your way of presenting your code.

Please keep in mind that this group is primarily for _discussing_ the
Perl programming language. Well defined questions about Perl are also
welcome - the answers to them may be helpful also to others besides
the persons who asked.

At the same time, posts basically saying "I found this (bad) code
somewhere on the web, and it doesn't work, please help me fix it" will
probably not be answered, at least not in the way the person who asked
had expected... People who ask questions are expected to have made
reasonable own efforts to solve their problems by help of e.g. the
Perl documentation and the Perl FAQ before asking for help.
I find the best way is to Jump in at the deep end. Sink or Swim.
And I like to learn as I am doing.

I understand that learning style, I have practiced it a lot. :)
However, it does not fit very well as a base for posting questions to
technical Usenet groups.
The Tutorials online are either the 'Hello World' type or extremely
complex, nothing in between. But ill keep plugging away until I can
do it.
If you want to reccommend a tutorial please feel free to.

I have no personal favorite. This URL may be useful:
http://cgi.resourceindex.com/Documentation/CGI_Tutorials/

However, as regards learning to understand how a piece of code works,
you need to know how to use the Perl documentation. If you have
installed Perl on your computer (which you really should do if you
haven't already), you have the whole documentation on your hard disk.
It's also available on line, e.g. at http://www.perldoc.com/. The FAQ
about CGI may be of special interest to you:
http://www.perldoc.com/perl5.8.0/pod/perlfaq9.html

Good luck!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top