in reply to extra spaces in textarea

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

Replies are listed 'Best First'.
(jeffa) 2Re: extra spaces in textarea
by jeffa (Bishop) on Jan 07, 2002 at 11:01 UTC
    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)
    
Re: Re: extra spaces in textarea
by ja (Initiate) on Jan 15, 2002 at 09:12 UTC
    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