| Author |
Strange output problem
|
|
|
|
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?
| |
| Matt Garrish 2006-09-26, 6:59 pm |
|
arod wrote:
> 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
| |
| usenet@DavidFilmer.com 2006-09-26, 6:59 pm |
| arod wrote:
> 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.
--
David Filmer (http://DavidFilmer.com)
| |
|
| OK yeah it actually was the input that was messed. Thanks.
usenet@DavidFilmer.com wrote:
> arod wrote:
>
> Show us what the input line looks like (from the array) that produces
> that output.
>
> --
> David Filmer (http://DavidFilmer.com)
| |
| DJ Stunks 2006-09-26, 6:59 pm |
| arod wrote:
> 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
| |
| Peter J. Holzer 2006-09-26, 6:59 pm |
| On 2006-09-26 20:34, arod <ajrodriguez@gmail.com> wrote:
>
> 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/&/&/;
$s =~ s/</</;
$s =~ s/>/>/; # only for symmetry
$s =~ s/"/&dquot;/; # only needed in attributes
$s =~ s/'/'/; # 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</td><td!>+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
--
_ | Peter J. Holzer | > Wieso sollte man etwas erfinden was nicht
|_|_) | Sy min WSR | > ist?
| | | hjp@hjp.at | Was sonst wäre der Sinn des Erfindens?
__/ | http://www.hjp.at/ | -- P. Einstein u. V. Gringmuth in desd
| |
| DJ Stunks 2006-09-26, 6:59 pm |
| Peter J. Holzer wrote:
> sub html_escape {
> my $s = @_;
> $s =~ s/&/&/;
> $s =~ s/</</;
> $s =~ s/>/>/; # only for symmetry
> $s =~ s/"/&dquot;/; # only needed in attributes
> $s =~ s/'/'/; # only needed in attributes
> return $s;
> }
use CGI qw{ escapeHTML };
-jp
|
|
|
|