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

I am trying to have text taken from a textarea and saved to a txt file. Then read the txt file and place the text back in the textarea. What is happening is an extra space is being placed at the start of all but the first line. So while the input starts off like this:

Hey wassup
wats wrong

It ends up like this:

Hey wassup
   wats wrong

Not a good look! The relevant code i am using is:

[See code in reply -- chipmunk]

Edit: chipmunk 2002-01-06

Replies are listed 'Best First'.
Re: extra spaces in textarea
by blakem (Monsignor) on Jan 07, 2002 at 12:21 UTC
    Are you sure your print statement is print @arr not print "@arr"? They have different default behavior for what gets printed between the elements...
    % perl -le '@arr = 1..10; print @arr; print "@arr"' 12345678910 1 2 3 4 5 6 7 8 9 10
    If you really are getting an extra space using print @arr you might want to check the value of $, which is usually an empty string...
    % perl -le '$, = ""; print 1..10' 12345678910 % perl -le 'print 1..10' 12345678910 % perl -le '$, = " "; print 1..10' 1 2 3 4 5 6 7 8 9 10 % perl -le '$, = " abc "; print 1..10' 1 abc 2 abc 3 abc 4 abc 5 abc 6 abc 7 abc 8 abc 9 abc 10
    If someone has globally modified the value of $, they deserve to be slapped with a trout, but a quick fix for you is to locally reset the value where its causing you problems:
    { # set up new enclosing scope since we are modifying globals local $,; # temporarilly set $, back to the empty string print "<center><textarea name=head rows=6 cols=60 wrap=soft>\n"; print @linesHead; # <- wont see extra spaces here... print "</textarea></center>\n"; } # end enclosing scope... $, is back to its old value

    -Blake

      Thank you all for your ideas and input. I have tried your suggestions to no avail. The extra space is being added when the form entry is being written to the text file ie in the saves part. This is evident when I take a look at the text file.

      I have tried this script on multiple servers and with different browsers with the same result. Im sure it does not have quotes.

      jeffa i do not understand alot of what you have said. Why is it bad to mix html with perl?? Is this a functional or speed issue?? Of interest I tried adding use(strict) and got pages of errors.

      Anyone have a magic bullet??
      James
Re: extra spaces in textarea
by jonjacobmoon (Pilgrim) on Jan 07, 2002 at 11:35 UTC
    For what is worth, I couldn't reproduce your problem either.

    Here is the script I used:

    #! /usr/bin/perl -Tw use strict; use CGI qw(:standard); # for parsing the input from the HTML my @linesHead; ##### saves data to file open (FILE,">/home/league/currentHeader.txt") or die "cannot open file +: $!"; flock (FILE, 2) or die "cannot lock file exclusively: $!"; print FILE param('head'); close(FILE); #### reads data from file open(FILE,"</home/league/currentHeader.txt") or die "cannot read data: + $!"; @linesHead = <FILE>; close(FILE); ### displays code in textarea print "content-type: text/html\n\n"; print "<html><body>"; print "<center><form><textarea name=head rows=6 cols=60 wrap=soft>"; print @linesHead; print "<\/textarea><\/form><\/center>"; print "<\/body><\/html>";

    I tried to used at least part of what you did so I could try and recreate the problem, but as I said could not. I do agree that you should not be using an array in this way nor should you be embedding the HTML with the Perl.

    Still, to your point. Have you tried a variety of browsers? Have you tried changing the wrap to virtual or just off altogehter. Just grasping at straws.....


    I admit it, I am Paco.
Re: extra spaces in textarea
by innerfire (Novice) on Jan 07, 2002 at 09:21 UTC

    I can't reproduce your problem, but I have had something like it in the past. I think it is due to Perl separating the elements of the array with a space for viewing. Try this:

    print foreach @ary;

    You might also try slurping up the file into a big scalar instead of line-by-line into an array.

    http://www.nodewarrior.org/chris/

Re: extra spaces in textarea
by ja (Initiate) on Jan 07, 2002 at 09:05 UTC
    sorry this is my first post here is the code:

    ### displays code in textarea print "<center><textarea name=head rows=6 cols=60 wrap=soft>\n"; print @linesHead; print "</textarea></center>\n"; ### saves data to file open (FILE,">$dataDir/currentHeader.txt"); flock (FILE, 2) or die "cannot lock file exclusively: $!"; print FILE $FORM{'head'}; close(FILE); ### reads data from file open(FILE,"< $dataDir/currentHeader.txt"); @linesHead = <FILE>; close(FILE);

    I have searched for hours and tried all sorts of ways to correct this. While I have seen others with the same problem I am yet to see an answer that works.

    Cheers James
      I recommend not printing an array like you do. Instead, i would opt for a scalar. One way to get a scalar from a file handle (instead of an array) is to join the array on an empty string.

      Also, use CGI!!!! Try this instead:

      use CGI qw(:standard); my $file = 'foo.txt'; my $lines = read_file($file); print header(), start_form(), textarea( -name => 'head', -value => $lines, -rows => 6, -cols => 60, -wrap => 'soft', -override => 1, ), p(submit()); my $head = strip(param('head')); print end_form(); if ($head) { write_file($file,$head); } sub read_file { my $file = shift; open(FILE,$file) or die "$file not readable: $!"; my @lines = <FILE>; close(FILE); return join('',@lines); } sub write_file { my ($file,$new_head) = @_; print STDERR "adding $new_head"; open (FILE,'>',$file) or die "$file not writeable"; flock (FILE, 2) or die "$file not lockable"; print FILE $new_head; close(FILE); } sub strip { my $param = shift; return '' unless $param; $param =~ s/^\s+//g; $param =~ s/\s+$//g; return $param; }
      The CGI module is used to handle generating the HTML form and parsing the query string parameters. All subroutines that you see that are not defined in this script are defined in CGI.pm - you can read more about CGI.pm here.

      Of interest is the strip() function which does some basic validation on the text to be written to the file. In this case, if nothing but whitespace or nothing at all is submitted then the file will not be written to.

      The read_file subroutine uses the join trick i mentioned earlier. This is just one way to convert an array into a scalar explicitly.

      Hope this helps.

      P.S. Oh! I really should mention something about the -override option to the textarea() method. CGI.pm by default will use the value that was submitted. Be sure that you specify -override in this situation because you are wanted to show the contents of the file, not what was submitted. Comment that line out and see what i mean.

      Also, don't forget to properly set file and directory permissions/ownerships.

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      F--F--F--F--F--F--F--F--
      (the triplet paradiddle)
      
      I believe I have traced my problem to using $FORM{'head'} rather than param('head') as the two replies have used. However when I try to use it in the same way I end up with an empty file. I have added use CGI qw(:standard); to the top of the script.

      ##### saves data to file open (FILE,">/home/league/currentHeader.txt") or die "cannot open file +: $!"; flock (FILE, 2) or die "cannot lock file exclusively: $!"; print FILE param('head'); close(FILE);


      I have tried the code above which I thought looked right but the file remains empty. Also tried declaring it first like this:

      $heado = new CGI; $headt=$heado->param('head'); open (HEAD,">$dataDir/currentHeader.txt"); flock (HEAD, 2) or die "cannot lock file exclusively: $!"; print HEAD $headt; close(HEAD);


      What am I missing???
      James