Strange output problem

A

arod

I'm trying to output some data into an html table with this code:

print "\n";
for my $array ( @sorted ) {
print "<tr>";
for my $element ( @$array ) {
print "<td>" . $element . "</td>";
}
print "</tr>";
print "\n";
}

However after successfully outputting several rows it sometimes
randomly throws in a row like this:

<tr><td>CY</td><td>2.4321</td><td>16.79</td><td!>+0.04</td></tr>
where one of the <td> tags is instead <td!> which is clearly wrong.

Any idea?
 
M

Matt Garrish

arod said:
I'm trying to output some data into an html table with this code:

print "\n";
for my $array ( @sorted ) {
print "<tr>";
for my $element ( @$array ) {
print "<td>" . $element . "</td>";
However after successfully outputting several rows it sometimes
randomly throws in a row like this:

<tr><td>CY</td><td>2.4321</td><td>16.79</td><td!>+0.04</td></tr>
where one of the <td> tags is instead <td!> which is clearly wrong.

Always start with the obvious. If that is your actual code, then are
you sure that $element isn't "16.79</td><td!>+0.04"? If you're parsing
this data out from some other html than it's entirely possible whatever
cleanup you're doing on it didn't remove the invalid tag.

Matt
 
U

usenet

arod said:
where one of the <td> tags is instead <td!> which is clearly wrong.

Show us what the input line looks like (from the array) that produces
that output.
 
D

DJ Stunks

arod said:
I'm trying to output some data into an html table with this code:

print "\n";
for my $array ( @sorted ) {
print "<tr>";
for my $element ( @$array ) {
print "<td>" . $element . "</td>";
}
print "</tr>";
print "\n";
}

However after successfully outputting several rows it sometimes
randomly throws in a row like this:

<tr><td>CY</td><td>2.4321</td><td>16.79</td><td!>+0.04</td></tr>
where one of the <td> tags is instead <td!> which is clearly wrong.

Any idea?

Perl is an interesting language. Once the interpreter went self aware
(August 2006) it started just trying to mess with programmers. I've
had so many random exclamation points inserted that I think I'm going
to switch to Ruby.

-jp
 
P

Peter J. Holzer

I'm trying to output some data into an html table with this code:

print "\n";
for my $array ( @sorted ) {
print "<tr>";
for my $element ( @$array ) {
print "<td>" . $element . "</td>";
}
print "</tr>";
print "\n";
}

However after successfully outputting several rows it sometimes
randomly throws in a row like this:

<tr><td>CY</td><td>2.4321</td><td>16.79</td><td!>+0.04</td></tr>
where one of the <td> tags is instead <td!> which is clearly wrong.

It is also clearly impossible unless the "<td!>" is included in the
data. For example, if

@sorted = (
[ 'CY', 2.4321, '16.79</td><td!>+0.04' ],
);

your code will print the line above.
Any idea?

First, wenn producing HTML, always escape any data. Instead of

print "<td>" . $element . "</td>";

use

print "<td>" . html_escape($element) . "</td>";
...
sub html_escape {
my $s = @_;
$s =~ s/&/&amp;/;
$s =~ s/</&lt;/;
$s =~ s/>/&gt;/; # only for symmetry
$s =~ s/"/&dquot;/; # only needed in attributes
$s =~ s/'/&apos;/; # only needed in attributes
return $s;
}

For the same data in @sorted this would produce:

<tr><td>CY</td><td>2.4321</td><td>16.79&lt;/td&gt;&lt;td!&gt;+0.04</td></tr>

which makes it easy to spot the error. It also makes
cross-site-scripting attacks a lot harder.

Second, I've found it helpful to insert debug output in comments, for
example, something like:

my $row = 0;
for my $array ( @sorted ) {
print "<!-- row $row -->";
print "<tr>";
my $col = 0;
for my $element ( @$array ) {
print "<!-- col $col: $element -->";
print "<td>" . $element . "</td>";
$col++;
}
print "</tr>";
print "\n";
$row++;
}

makes it easier so see where the error occurs. This helps you to collect
data for a test case.

Finally, try to make the error reproducable. If something happens
"randomly", it often only means that you haven't found the cause yet.
Trim down your code to the minimal code which still exhibits the
behaviour. Don't get your data from sources which may change from one
run of your program to the next (e.g., websites, log files, etc.). Get
it from sample files or enter it directly into your program.

hp
 
D

DJ Stunks

Peter said:
sub html_escape {
my $s = @_;
$s =~ s/&/&amp;/;
$s =~ s/</&lt;/;
$s =~ s/>/&gt;/; # only for symmetry
$s =~ s/"/&dquot;/; # only needed in attributes
$s =~ s/'/&apos;/; # only needed in attributes
return $s;
}

use CGI qw{ escapeHTML };

-jp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,654
Latest member
LannySinge

Latest Threads

Top