reading and writing to text file via form newbie

Discussion in 'Perl Misc' started by Trotsky, Nov 22, 2003.

  1. Trotsky

    Trotsky Guest

    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
    Trotsky, Nov 22, 2003
    #1
    1. Advertising

  2. 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
    Gunnar Hjalmarsson, Nov 22, 2003
    #2
    1. Advertising

  3. Trotsky

    M Guest

    "Gunnar Hjalmarsson" <> wrote in message
    news:bpojil$1r7ah1$-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
    M, Nov 23, 2003
    #3
  4. 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
    Gunnar Hjalmarsson, Nov 23, 2003
    #4
  5. Trotsky

    Trotsky Guest

    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

    >
    Trotsky, Nov 24, 2003
    #5
  6. 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/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!

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Nov 24, 2003
    #6
  7. Trotsky

    Brad Baxter Guest

    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
    Brad Baxter, Nov 24, 2003
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Hearn
    Replies:
    1
    Views:
    313
    avnrao
    Jun 30, 2004
  2. HNguyen
    Replies:
    4
    Views:
    2,388
    HNguyen
    Dec 21, 2004
  3. jennyw
    Replies:
    0
    Views:
    315
    jennyw
    Jul 11, 2003
  4. Skip Montanaro
    Replies:
    0
    Views:
    423
    Skip Montanaro
    Jul 11, 2003
  5. icedragon
    Replies:
    0
    Views:
    640
    icedragon
    Jul 13, 2010
Loading...

Share This Page