Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

script adding spaces into a file for no reason...

by JoeG (Initiate)
on May 20, 2000 at 00:17 UTC ( [id://13415]=perlquestion: print w/replies, xml ) Need Help??

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

I have written a perl script to basically allow the editing of a cleartext file online. Everytime you click the submit button, regardless of changes, it adds spaces "magically" (unfortunately) to the beginning of every line??

Very strange.....

Please help. :)
The actual script: The readable script:
Thanks much!
  • Comment on script adding spaces into a file for no reason...

Replies are listed 'Best First'.
Re: script adding spaces into a file for no reason...
by httptech (Chaplain) on May 20, 2000 at 00:40 UTC
    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>

      It worked!

      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?

        I don't see any extra newlines... Where are they appearing?
      <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.

        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?>

Re: script adding spaces into a file for no reason...
by arcterex (Acolyte) on May 20, 2000 at 01:55 UTC
    Could you not do something like:

    Convert \n to a BR tag when you save, then when you display the file convert the BR's to newlines? ie: <CODE> sub save { $text = $cgi->param("text"); $newtext = s/\n/
    /g; # save $newtext to somewhere... } # and when you retrieve sub load { $text = ... # get variable from file $newtext = s/
    /\n/g; print "<textarea>$newtext

RE: script adding spaces into a file for no reason...
by Anonymous Monk on May 20, 2000 at 02:41 UTC
    Submitting text from a text area adds a \n and a \r to the end of everyline... Strip the incoming text of \r and you'll be fine....
Re: script adding spaces into a file for no reason...
by turnstep (Parson) on May 20, 2000 at 00:37 UTC
    Seems to work for me...

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://13415]
Approved by root
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2024-07-12 11:59 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.