# First encode possible HTML, because the text is # supposed to be plain text use HTML::Entities(); $text = HTML::Entities::encode($text); # Convert Windows or Mac line terminators to Unix single \n $text =~ s/\r\n|\n\r/\n/g; # Remove leading and trailing white space $text =~ s/^\s*//; $text =~ s/\s*$//g # Convert all double line breaks to para tags $text =~ s|\n\s*\n|
|g
# Convert any single line breaks to br tags
$text =~ s|\n\s*|
|g
# If text is more than just white space, add opening and
# closing para tags
$text = "
$text
" if $text;