Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Constructing HTML tables with CGI.pm

by sravs448 (Acolyte)
on Sep 19, 2014 at 14:00 UTC ( [id://1101206]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks,
I am trying to create a html table. I followed the instructions in the Re: Constructing HTML tables with CGI.pm
What am I missing here?
#!/usr/local/bin/perl -w use strict; use CGI; my %host=( 'camel', 'flea', 'frog', 'green'); cgiout(); sub cgiout { my $q= new CGI; my $tablecontent=[$q->th(['key', 'value'])]; for (sort keys %host) { push @$tablecontent, $q->td([ $_, $host{$_} ]) ; } print $q->table( { border => 1, -width => '100%'}, $q->Tr( $tablecontent), ); }

The output I got

<table width="100%" border="1"><tr><th>key</th> <th>value</th></tr> <t +r><td>camel</td> <td>flea</td></tr> <tr><td>frog</td> <td>green</td>< +/tr></table>

Replies are listed 'Best First'.
Re: Constructing HTML tables with CGI.pm
by choroba (Cardinal) on Sep 19, 2014 at 14:03 UTC
    Seems like a missing header (not mentioning start_html). See CGI for details.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Even after I adding those tags, I couldnt get output in a table format

      Modified Code

      #!/usr/local/bin/perl -w use strict; use CGI; my %host=( 'camel', 'flea', 'frog', 'green'); cgiout(); sub cgiout { my $q= new CGI; print $q->header; print $q->start_html; my $tablecontent=[$q->th(['key', 'value'])]; for (sort keys %host) { push @$tablecontent, $q->td([ $_, $host{$_} ]) ; } print $q->table( { border => 1, -width => '100%'}, $q->Tr( $tablecontent), ); print $q->end_html; }

      Output

      Content-Type: text/html; charset=ISO-8859-1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-U +S"> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1 +" /> </head> <body> <table width="100%" border="1"><tr><th>key</th> <th>value</th></tr> <t +r><td>camel</td> <td>flea</td></tr> <tr><td>frog</td> <td>green</td>< +/tr></table> </body> </html>
        Just to make sure, do you run the script on a web server and view the result in a browser?
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

        Even after I adding those tags, I couldnt get output in a table format

        The output you show is "html tables" -- what were you expecting?
        key value
        camel flea
        frog green

Re: Constructing HTML tables with CGI.pm
by NetWallah (Canon) on Sep 19, 2014 at 18:20 UTC
    Are you expecting 4 wooden legs, and a flat surface ? :-)

            "You're only given one little spark of madness. You mustn't lose it."         - Robin Williams

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-04-23 11:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found