Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

HTML Table Using Perl

by Kiko (Scribe)
on Oct 26, 2000 at 04:30 UTC ( [id://38528]=perlquestion: print w/replies, xml ) Need Help??

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

Hey, I'm trying to create an HTML table of the powers of 2,3, and 4. It se +ems to be working. The only problem is that the html_table() subrout +ine is printing out both calls that i made to it ($table1, and $table +2), instead of just one ($table2). Thanks, Kiko #!/perl/bin/perl print "Content-type: text/html\r\n\r\n"; # generate an HTML table of the powers of 2,3 and 4 $rows[0] = html_row("white"," n " , "nth power of 2", "nth power of 3", "nth power of 4"); for ($i=0;$i<10;$i++) { if ($i % 2) { $rows[$i+1] = html_row("#cccccc",$i,2**$i,3**$i,4**$i); } else { $rows[$i+1] = html_row("#ccccff",$i,2**$i,3**$i,4**$i); } } $table = html_table(1,"",@rows); $table2 = html_table(0,"black",html_row("",$table)); print "Here are the powers of 2, 3 and 4<BR><BR>\n"; print "$table2"; # This subroutine will print out a table row sub html_row { local($color, @row)=@_; $rowP.="<TR BGCOLOR=$color>"; foreach $a (@row) { $rowP.="<TD> $a </TD>"; } $rowP.="</TR>\n"; return ($rowP); } # This subroutine will print out an HTML Table sub html_table { local($border,$color,@table_row)=@_; $tableP.="<TABLE BORDER=$border BGCOLOR=$color>\n"; foreach $b (@table_row) { $tableP.="$b"; } $tableP.="<TABLE>\n"; return ($tableP); }

Replies are listed 'Best First'.
Re: HTML Table Using Perl
by chromatic (Archbishop) on Oct 26, 2000 at 04:55 UTC
    You're appending to a global variable in html_row. Quit that! :)

    You want a new $rowP and $tableP on each subroutine call. Using the -w flag and strict is not just a good idea, it's almost a requirement for CGI scripts in Perl. Here's the fixed code:

    #!/usr/bin/perl -w use strict; print "Content-type: text/html\r\n\r\n"; # generate an HTML table of the powers of 2,3 and 4 my @rows = html_row("white"," n ", "nth power of 2", "nth power of 3", "nth power of 4"); for (0 .. 10) { if ($_ % 2) { push @rows, html_row("#cccccc",$_,2**$_,3**$_,4**$_); } else { push @rows, html_row("#ccccff",$_,2**$_,3**$_,4**$_); } } my $table = html_table(1,"",@rows); my $table2 = html_table(0,"black",html_row("",$table)); print "Here are the powers of 2, 3 and 4<BR><BR>\n"; print "$table2"; # This subroutine will print out a table row sub html_row { my $color = shift; my $rowP = "<TR BGCOLOR=$color><TD>"; $rowP .= join("</TD><TD>", @_) . "</TD></TR>\n"; return $rowP; } # This subroutine will print out an HTML Table sub html_table { my $border = shift; my $color = shift; my $tableP ="<TABLE BORDER=$border BGCOLOR=$color>\n"; $tableP .= join('', @_) . "</TABLE>\n"; return $tableP; }
    Note that this code is a lot shorter, and makes use of happy magic variables like @_.

    The biggest change is declaring all of the variables with my. This is what fixed your error. Oh, and the closing table tag bug is fixed in my version too.

      This is exactly why we have things like the strict pragma. PLEASE, if you have any unusual behavior in your scripts, be certain that you are useing strict and that your code generates no warnings with -w. This would have caught your problem (or at least forced you to think about the scoping of this variable, which I would hope would have brought attention to the issue)!
      You may also want to add a "sanity check" to the allocation of values to the BORDER and BGCOLOR lines too. Maybe even surround the values by double quotes, just in case something wierd gets passed to them which may break the HTML.
Re: HTML Table Using Perl
by Maclir (Curate) on Oct 26, 2000 at 04:54 UTC
    Before everyone else says "Use CGI.pm", your html_table sub has a logic bug - you are not closing the table (the statement just before the return). You have
    $tableP.="<TABLE>\n";
    when you should have
    $tableP.="</TABLE>\n";
    Depending on your browser, this can really stuff up the appearance of the HTML.

    Update:As well, you are not generating compliant HTML code. For example, you are missing the normal HEAD and BODY tags. While some browsers can handle these omissions, and generally interpolate them, this is not a good idea. It you really want to continue with your own "hand generated" code, you may want to have a "start_html" and "end_html" subroutines that do all the other html bumph.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://38528]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2024-03-28 12:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found