cdguitar01 has asked for the wisdom of the Perl Monks concerning the following question:

- I'm basically using a PHP include to put the contents of a .txt file (in this case u_texas.txt) into an editable textarea. When the user makes changes to the textarea and submits it, the new text replaces the old text in the file.
- So far so good.
- The problem comes in when you go back and edit the file again. Every time, everywhere there is a new line, it adds another.
(e.g)
startsbecomesbecomes
1
2
3
1

2

3
1


2


3
and so on and so on.

I'm assuming this is caused by the \n characters being added on to each line of the STDIN, but I don't think chomp works in this case, at least I haven't been able to make it work.

This is the perl script which updates the file by recieving two form parameters; a file name, and the actual text that was in the text area.

#! c:\perl\bin\perl.exe -w use strict; use CGI; my $query = CGI->new(); my @names = $query->param; my $file_name = $query->param( $names[0] ); my $file_path = '../htdocs/safe/internship/u_texas.txt'; my $new_text = $query->param( $names[1]); chomp(my $success = open OUT, ">$file_path"); # test for open omitted for space concerns print OUT $new_text; close OUT;
How does one perform a chomp on each line of text as it comes in, is that even possible, or am I just thinking about this the completely wrong way and if so, how should I be going about this process.

Thanks,
Chris (Boston,USA)

Edit by dws to add <code> tags

Replies are listed 'Best First'.
Re: Extra (\n)'s in the file I'm updating?
by valdez (Monsignor) on Jan 11, 2003 at 11:08 UTC
      Thanks for the links, but after trying a few solutions with no success, I just decided to create the entire application in perl instead.

      Thanks,
      Chris (boston, USA)
Re: Extra (\n)'s in the file I'm updating?
by vek (Prior) on Jan 11, 2003 at 15:10 UTC
    Couple of things to point out here. First of all, one of the lines in your code isn't doing what you think it's doing:
    chomp(my $success = open OUT, ">$file_path"); # test for open omitted +for space concerns
    open returns 1 on success and 0 on failure. What you're doing here is assigning the return value from open to the $success variable and then chomping it. For future reference always check the return value from open like this:
    open(OUT, $file_path) || die "Couldn't open $file_path - $!\n";
    Secondly, you are blindly accepting and using parameters without un-tainting them. If you don't know what this means, check out Ovid's CGI course, particularly the security section.

    Lastly, to answer your original question, if the newlines are at the end you need to be chomping the $new_text variable prior to printing it to $file_path.

    -- vek --
      1st. Your right about the misplaced chomp. I seem to have posted a slightly older version of my code, that was simply an attempt. But it returned 1 just like it should have, so I put it back.

      2nd. Thanks for the open or die line, I hadn't been doing it like that before, my way took roughly 8 lines I think.

      3rd. I know about un-tainting from the first few chapters of the Ovid CGI course, but I never finished it because I moved to the O'Reilly Learning Perl book. I'm going to finish Ovid's once I'm done that book.
      No one can access this document from the outside world other then me, so for now I think I'm pretty safe.

      Thanks,
      Chris (boston,USA)
Re: Extra (\n)'s in the file I'm updating?
by poj (Abbot) on Jan 11, 2003 at 13:24 UTC
    This line will 'squash' any multiple \n characters into only one
    $new_text =~ tr/\n//s;
    Also, using $names[1] means your code relies on a very specific page layout of the elements to work.
    poj
      Thank you for the code, I've changed how I'm executing this application so I'm not sure it's going to apply anymore, but thanks anyways.

      Oh, and as for the $names[1] requiring very specific page layout, I know. It's ok for now because I'm the only person using the code, but I know I shouldn't do it this way. I've just been jumping ahead in my learning of Perl because I see different things I want to try. I just haven't learned the appropiate way of aquiring name/value pairs. Thats pretty high on the list of things to learn.

      Thanks,
      Chris (boston,USA)
Re: Extra (\n)'s in the file I'm updating?
by Chady (Priest) on Jan 11, 2003 at 10:50 UTC

    you don't need chomp(my $succes.... you just need to chomp the $new_text : chomp(my $new_text = $query->param( $names[1] ))

    Update: In fact, it sounds as if it's PHP's fault.. Perl is just printing what it recieved, so you must take whichever \n you are adding to the text in the PHP code.


    He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

    Chady | http://chady.net/
      You where right about it being PHP's fault, it looks like by using the include inside a textarea it formatted it incorrectly. Thanks for your help. I solved the problem by simply making the whole application in perl instead of using Perl, it works fine now.

      Thanks again
      Chris (boston,USA)