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

I want to retrieve and print the contents of a Text Area exactly as it was input from a keyboard, i.e. with no HTML formatting. I am using the code noted below. The content is retrieved and printed but as a single line when it was entered as multiple paragraphs. This is the code I am using

use strict; use warnings; use CGI; my $q = CGI->new; # Print the HTTP header print $q->header; # Get the content of the text area. # Replace 'textarea_name' with the actual 'name' attribute of your HTM +L textarea. my $textarea_content = $q->param('message'); # Print the content exactly as received ####print "Content of the text area:\n"; print $textarea_content;

When content is entered as HTML it is returned as originally entered. Otherwise the return is one line. Is there a way to solve my problem? Thanks for any and all help!

Replies are listed 'Best First'.
Re: Retrieve and Print TextArea Content
by Corion (Patriarch) on Sep 12, 2025 at 18:23 UTC

    You are outputting the text as you receive it.

    This is how HTML works. HTML knows nothing about normal linebreaks. Whitespace in text does not break lines in HTML.

    If you want to output paragraphs from your text in HTML, you need to convert all linebreaks to <br> tags, or wrap paragraphs in <p>...</p> pairs, or output the text in <pre> tags. Here is how to add <br> tags:

    my $textarea_content = $q->param('message'); # Escape all characters that are special for HTML my %html_escape = ( '<' => '&lt;', '>' => '&gt;', '&' => '&amp;', ); $textarea_content =~ s/([<>&])/$html_escape{ $1 }/ge; # Convert newlines to <br> tags $textarea_content =~ s/\r?\n/<br>\n/g; print $textarea_content;