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

Hello again! I built a page for my teacher that loads the contents of a file into a textarea in a form on a web page so he can edit some links from any browser. If the text file is empty, the script that displays the links jumps to a subroutine that says, "no links at this time"

This is only happening when I delete the contents of the file on the server. If I do it by clearing the contents of the textarea through a brower and pass them to the script, there is always 1 blank character.

I am still new to all of this, but am reading a book right now and trying things like chop, chomp, truncate.. ?

Am I close? Or have I gone completely mad?!

Here is the code that writes what was in the textarea to file:

#!/usr/bin/perl use CGI::Carp qw(fatalsToBrowser); use strict; use CGI ':standard'; my $list; my @changes = param('tellmessage'); open(FILE, ">eetnlinks.txt") || die "Can't open file!"; flock(FILE, 2) || die "Can't lock file!"; #truncate(FILE,0); foreach $list (@changes) { chomp ($list); print FILE "$list\n"; } close(FILE);

Replies are listed 'Best First'.
Re: Always leaving one space in file
by zby (Vicar) on Apr 23, 2003 at 15:07 UTC
    From your description it seems that the tellmessage parameter is a textarea - but you use it as if it was a multiple choice scrolling list.

    And to answer your question - the my @changes = param('tellmessage'); sets the @changes list to a one element list (if the tellmessage parameter is empty than the list contains one empty string). Thus the foreach loop allways is executed once - and when the parameter is empty the print statement prints the "\n" character.

      I tried this with no success:

      my $list; my @changes = param('tellmessage'); ###################### if (@changes eq " ") { exit } ###################### open(FILE, ">eetnlinks.txt") || die "Can't open file!"; flock(FILE, 2) || die "Can't lock file!"; #truncate(FILE,0); foreach $list (@changes) { chomp ($list); print FILE "$list\n"; } close(FILE);
      Or could I count the length of @changes and compare it:
      if (@changes > 1) { exit; }
      If that would work, I don't know how to count the length of the string... ?
        Try not opening the file unless you have data in the @changes array:
        if (@changes) { # do stuff here }
        I don't know how to count the length of the string...?:
        my $length = length $some_string;
        -- vek --
        Why do you insist on a list? Does the tellmesage parameter contain multiple values? If it was a textarea than it contains just one value and you can retrieve it with:
        my $change = param('tellmessage');
        what I meant was:
        if (@changes eq " ") { @changes eq ""} }