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

I've got a textarea where the user is supposed to submit some text. When the text is submitted, words are joined by a + sign. To get rid of them I use    $var =~ s/\+/ /;    Problem is that it also weeds out those pluses that are entered by the user and supposed to stay.

Any way around it?

Also, how would I preserve the exact formatting that the user enters?

Replies are listed 'Best First'.
RE: splitting question
by neshura (Chaplain) on May 21, 2000 at 23:58 UTC
    Sure. Read up on and make liberal use of the CGI perl module -- it allows you to retrieve the information intact quite magically as shown below...
    $usertext = $cgi->param('sometextarea');

    e-mail neshura

      This cannot be overstated. Until you know why you don't need CGI.pm, always use CGI.pm.
RE: splitting question
by turnstep (Parson) on May 22, 2000 at 00:38 UTC

    Read the above, but as far as your example code, don't forget your 'g' at the end of your regexp so that you replace more than one "plus"...

    $var =~ s/\+/ /g;
    Even better, just use the 'tr' function:
    $var =~ tr/+/ /;
    For the record, the pluses should be escaped by the browser into "%2B", and you should not have to worry about the user's pluses.

      Or instead of subs on each one of the special values (like %20, or %2B), you can try this:
      s/%(..)/pack("C", hex($1))/eg;
      Actually, forget the above and go with CGI.pm. Its much easier...
        It was spoken:
         Or instead of subs on each one of the special values (like %20, or %2B), you can try this:
        s/%(..)/pack("C", hex($1))/eg;
        Actually, forget the above and go with CGI.pm. Its much easier...
        Arrrgh! Please, any code that uses pack there was written in the Perl4 days, before we had chr in Perl5. Are you PurlGurl? :)

        And of course, the proper thing is yes, use CGI.pm, please.

Using CGI.pm
by The Alien (Sexton) on May 22, 2000 at 09:54 UTC
    Using CGI.pm would seem to be especially important in this case. I do not mean this as an insult at all, but if you didn't know about the translation of the plus character, you probably don't know about the other weirdness of encoding used for GET method form submissions. CGI.pm can translate it seamlessly. While understanding the protocol is very useful, true understanding usually leads to the knowledge that it's a pain to implement.