Perl/cgi problem

I am new to the world of programming and have taken up perl and cgi. I wrote a program to read form data submitted on my comments page, write them to a .dat file, then print the data into a comment box. The problem is whenever someone makes a new line/paragraph the data after the first paragraph doesn’t get printed.

Here is the part I need fixed:

read(STDIN, $info, $ENV{‘CONTENT_LENGTH’});
@pairs = split(/&/, $info);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/ / /;
$value =~ s/%([a-zA-Z0-9][a-zA-Z0-9])/pack(“C”, hex($1))/eg;
$FORM{$name} = $value;

I tried to replacing the new line into a break tag like so:

$value =~ s/\n/break tag here/;
$value =~ s/\r/break tag here/;


$value =~ s/
/break tag here/;

none work. Does anyone know a way to turn all new lines into a break tag?

Brad Campbell

Something like

$value =~ s/[\n\r]/br/g;should work.

I’m not sure why what you’ve got isn’t working. It might be easier to debug if you post the whole script.

Incidentally, it’s probably easier to just use the standard CGI module. For info, do:

perldoc CGIYou can then use the param method to get at parameters. Also, you’ll want to use the escapeHTML method before displaying user-supplied data. You’ll need to do this before converting newlines and carriage returns in HTML breaks.

So your code might just become something like:

[code] use CGI qw(param escapeHTML);

my $comments = param(‘comments’) || ‘’;
$comments = escapeHTML($comments);
$comments =~ s/[\n\r]/br/g;
print $comments;[/code]Edit: I’ve just used br for the br tag, because you don’t seem to be able to add angle brackets to this forum.


Sorry. Didn’t work.

Here is the url to a zip folder containing the comments page cgi for reading and posting comments and for writing the posted info to the .dat file:

Also I just found out from my friend that when he submits a comment it brings him to a 500 internal error page, but it still posts the first paragraph of his comment. When I submit a comment it works fine except for the problem I mentioned in the original post.

Here is a test page for anyone to test the script:

Brad Campbell

Try swapping these two lines:

$value =~ s/[\n\r]/br/g;
$value =~ s/%([a-zA-Z0-9][a-zA-Z0-9])/pack(“C”, hex($1))/eg;

But really you’d be better off using the standard CGI module.

Prloblem fixed. Thanks Ian for all the help.

Brad Campbell