http://qs1969.pair.com?node_id=7624

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

In perl (using cgi), what is the code for a return?
I know it should be '\n', but when I take what's in a textarea box, and use
$variable =~ s/\n//eg;
it doesn't remove all of the returns.
Is something wrong with that little bit of code?
What I want to do Iis remove all of the carriage returns, so that if the user simply puts carriage returns in the textarea box, the variable will hold nothing, so that I could use
if ($variable eq "") { $variable = "Some Default Message"; }
If someone helps me, I'd appreciate it!

Replies are listed 'Best First'.
Re: Return?
by comatose (Monk) on Apr 14, 2000 at 19:17 UTC

    In perl, there is no code for starting the next line of text. It is something that the operating system determines. That's why you have to use \r\n for a new line (not \n) in MS products, \n for Unices, and \r for Macs. There might even be something out there that wants \n\n or something even weirder.

    In any case, to avoid any wackiness, you'll want to make sure the data is treated as a single line.

    # We don't want to put text right up against itself so # replace with a space rather than nothing. /s treats it # all as a single line - very important. $message =~ s/\r\n|\n|\r/ /gs;
      Wouldn't:
      $message =~ s/\r|\n//g;
      do the same thing as:
      $message =~ s/\r\n|\n\r//g;

      Your humble servant
      -Chuck
        No it wont, The first one will blow away any newline or carrige return in the text
        The second will only remove cr/lf pairs.
Re: Return?
by chromatic (Archbishop) on Apr 14, 2000 at 19:30 UTC
    A similar question is in ASCII to HTML. To answer your direct question, I would use: $message =~ tr/\n\r//d; This deletes any linefeeds or carriage returns. It's much more efficient than the s/// operator. If you want to format the text for display, you might use btrott's oneliner: $message = join "\n<p>\n", grep s/\n/<br>\n/g || 1, split /\n\n/, $message;
Re: Return?
by btrott (Parson) on Apr 14, 2000 at 19:23 UTC
    You may also think about using a different technique than just testing for carriage returns; it sounds like you'd like to use the default variable basically if the user didn't post any "good text" in the text box. So if they just posted whitespace (whether that be tabs, spaces, or carriage returns), you'd like to use the default variable.

    In that case, use something like this:

    $variable = "Some Default Messsage" unless $variable =~ /\S/;
    This sets variable to the default unless it contains a non-whitespace character.
Re: Return?
by Anonymous Monk on Apr 14, 2000 at 22:49 UTC
    Ok, thanx, I solved that problem!

    Now this:
    How can I see if the textbox contains ONLY spaces and nothing else?
    I don't need code that checks for carriage returns and all, just for spaces.

      You mean just the space character? Not for any whitespace?

      To check if a string contains only space characters, use

      my $string = " "; $string = "Default" if $string =~ /^ *$/;
      This sets $string to "Default" if $string consists of 0 or more space characters, and only space characters; if $string is " a " or something such, the regular expression won't match.

      If you're looking to match strings that consist only of whitespace (no non-whitespace characters), use:

      my $string = " \t \n "; $string = "Default" if $string =~ /^\s*$/;
      \s matches whitespace, so this does the same as the last regex, but matches any whitespace (not just space characters).
Re: Return?
by wouter (Sexton) on Apr 14, 2000 at 18:52 UTC

    I ran into the same problem a year ago while writing my first 'big' cgi script... I had a textarea called 'message', and i wanted to change newlines in
    breaks. This is how i did it:

    # change newline into 
    , and return into nothing $message =~ s/\n/
    /g; $message =~ s/\r//g;

    I figure the problem is that on a Windows platform, window is sending a linefeed AND hard return, while *NIX is linefeed only (and mac being hard return only, but haven't tested that out yet)

      Just an FYI .. The returns on the different platforms are:
      *nix: \n Mac: \r Windows: \r\n

      That it :-)

      philip

Re: Return?
by wouter (Sexton) on Apr 14, 2000 at 18:52 UTC

    I ran into the same problem a year ago while writing my first 'big' cgi script... I had a textarea called 'message', and i wanted to change newlines in <BR> breaks. This is how i did it:

    # change newline into <BR>, and return into nothing
    
    $message =~ s/\n/<BR>/g;
    $message =~ s/\r//g;
    

    I figure the problem is that on a Windows platform, window is sending a linefeed AND hard return, while *NIX is linefeed only (and mac being hard return only, but haven't tested that out yet)