http://www.perlmonks.org?node_id=13423


in reply to script adding spaces into a file for no reason...

When you print an array like
print <<END; <textarea>@text</textarea> END
it will print all lines of the array joined by a space. Instead, you can use: <textarea>@{[join("",@text)]}</textarea>

Replies are listed 'Best First'.
RE: Re: script adding spaces into a file for no reason...
by JoeG (Initiate) on May 20, 2000 at 00:44 UTC
    Thanks!

    It worked!

    Joe
RE: Re: script adding spaces into a file for no reason...
by JoeG (Initiate) on May 20, 2000 at 00:52 UTC
    oh.. wait.. ;-) Now its inserting like 3 ADDITIONAL paragraph marks (or hard returns) -- whatever you want to call them.. The only way I could tell was by pasting the code into MS Word and seeing them when I enabled "show P marks"..... any ideas for this problem?

    Joe
      I don't see any extra newlines... Where are they appearing?
        They're hidden ... MS Word sees them when i paste, and apparantly my news app sees them.. because it's really messing up the output.. ;-(
RE: Re: script adding spaces into a file for no reason...
by Anonymous Monk on May 20, 2000 at 00:59 UTC
    <textarea>@{join("",@text)}</textarea> the proposed solution still returns an array; It needs to be taken out of array context. Instead of print <<END , try: print "<textarea>" . join ( "", @text ) ."</textarea>\n" ; This is presuming, of course, that the "\n" is already on the end of each element of @text. if not, you might want to join them with "\n". Also, be aware that some browsers may take it upon themselves to make each line ending a "\n\r" - especially if you cut and paste into the textarea. I make it a habit to: $input =~ s/\r//g ; On all submitted form values to clean it up.
      My solution, @{[join("",@text)]} does not return an array in this case. (Look at pg.239-241 of Effective Perl Programming for more examples of using this construct). It does use the anonymous array construct and the array dereferencing construct, but the net result is to interpret the code inside the brackets in whatever context it would have occured elsewhere outside of the constuct and the here-doc. So, because "join" returns a scalar, so does my code. It's just a quick and dirty way to interpolate a piece of code inside a larger print statement.

      Of course, there are many other ways you could accomplish the same thing, I just happen to like this because it keeps the amount of variable assignment to a minimum.

        Thanks to everyone for their help. It's working now. Here's the core file saving code for your reference. It removes unncessary line returns, etc.

        if (defined $query->param('Submit')) { open NEWNEWS, "> cgi/news.txt" || die "Cannot open file: $!"; $result = $query->param('content'); $result =~ s/\r//g; print NEWNEWS $result; close(NEWNEWS); print '<p>Saved your changes.</p>'; $query->delete('Submit'); }


        JoeG
      ack! (look at the html source to decipher previous reply)
      I tried reading the source .. and used that code-- it runs w/o errors, but unfortunately didn't fix..
      I used:
      print "<textarea>" . join ( "", @text ) ."</textarea>\n" ;
      hmm.. this is really strange. any other ideas?>

      Joe
        I'm not sure what the problem is, but I'll add a couple of ways to interpolate an array without spaces...
        print "<textarea>", @text, "</textarea>\n"; # (unless you've redefined $,)

        ...or...

        { local $"; print "<textarea>@text</textarea>\n"; }