Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > reading and writing to text file via form newbie

Reply
Thread Tools

reading and writing to text file via form newbie

 
 
Trotsky
Guest
Posts: n/a
 
      11-22-2003
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
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      11-22-2003
Trotsky wrote:
> 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.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

 
Reply With Quote
 
 
 
 
M
Guest
Posts: n/a
 
      11-23-2003

"Gunnar Hjalmarsson" <(E-Mail Removed)> wrote in message
news:bpojil$1r7ah1$(E-Mail Removed)-berlin.de...

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


My thoughts exactly...!?!!


mike


 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      11-23-2003
M wrote:
> Gunnar Hjalmarsson wrote:
>> That's a bad approach. Really bad. Never use such code unless you
>> don't understand what it does!!

>
> My thoughts exactly...!?!!
>


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

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

 
Reply With Quote
 
Trotsky
Guest
Posts: n/a
 
      11-24-2003
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

>

 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      11-24-2003
Trotsky wrote:
> 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/Documen...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!

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

 
Reply With Quote
 
Brad Baxter
Guest
Posts: n/a
 
      11-24-2003
On Sat, 22 Nov 2003, Gunnar Hjalmarsson wrote:

> Never use such code unless you
> don't understand what it does!!


Now THAT should be a T-shirt.

Regards,

Brad
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ASP.NET form action file - writing to text file icedragon ASP .Net 0 07-13-2010 01:48 PM
reading the DB vs. reading a text file...performance preference? Darrel ASP .Net 3 11-11-2004 02:27 PM
Newbie needs a tutorial on reading and writing to nodes in an XML file David Hearn ASP .Net 1 06-30-2004 12:58 PM
Re: Reading a DOS text file and writing out Mac Skip Montanaro Python 0 07-11-2003 06:05 PM
Reading a DOS text file and writing out Mac jennyw Python 0 07-11-2003 04:57 PM



Advertisments