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

Hello All,

I need help with a chomp question please.
My form asks the user to select a field name from a radio_group, which is then sent to a textarea. I haven't included all the code for ease of reading but the following lines are inside a start_multipart_form and the textarea insertion is carried out after a button press.
print $query->radio_group(-name=>'fields',-value=>[ @array ]); chomp($field1 = $query->param('fields')); $condition = "field \n" . $field1 . " : " . "="; #lots more at the end + of the string, but not relevant to the question. print $query->textarea( -name=>'finalCond',-value=>$condition,-rows=>1 +5,-cols=>80,-force=>1);
The first time the user selects the field, the chomp works fine, so the textarea would contain e.g.
age : = 55
But the second and subsequent times the chomp fails and the textarea would contain
age
: = 55
I don't want to see a newline between 'age' and ':'.

Any ideas?
Thanks.

Replies are listed 'Best First'.
Re: chomp works but then doesn't??
by AidanLee (Chaplain) on Dec 18, 2001 at 18:24 UTC

    As is often noted on this site is that for data validation it is safest to parse incoming data for valid forms, rather than trying to filter out unwanted things. That way you'll be guaranteed good data, even if you haven't thought of all the things that could go wrong.

    in this case, if all that is allowed in that field is letters, you could do a regex on the \w character class to grab only those characters. Magically your end-of-line problems go away.

Re: chomp works but then doesn't??
by hotshot (Prior) on Dec 18, 2001 at 17:25 UTC
    maybe you should check if you get also '\r' from the web, you will have to cut them too

    Hotshot
Re: chomp works but then doesn't??
by chip (Curate) on Dec 19, 2001 at 12:36 UTC
    You shouldn't assume ANYTHING about the newlines (if any) in a textarea field. Basically I suggest that whitespace in textareas be nuked from orbit. Failing that, you should be sure that all line ending you do get are converted to some known representation:

    for ($textarea_value) { s/^\s+//; s/\s+\z//; s/[\x0D\x0A]+/\n/g; }

    This isn't perfect, but it should help you get more consistent results with various web clients.

        -- Chip Salzenberg, Free-Floating Agent of Chaos