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

I'm looking for doc on how to use the CGI.pm methods to generatel table code. There is nothing on the http://stein.cshl.org/WWW/CGI/ website.

Replies are listed 'Best First'.
Re: How to do tables
by davorg (Chancellor) on Jan 23, 2007 at 15:06 UTC
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: How to do tables
by Melly (Chaplain) on Jan 23, 2007 at 15:18 UTC

    And, for later, HTML Tables - populating TD from an array might be useful...

    map{$a=1-$_/10;map{$d=$a;$e=$b=$_/20-2;map{($d,$e)=(2*$d*$e+$a,$e**2 -$d**2+$b);$c=$d**2+$e**2>4?$d=8:_}1..50;print$c}0..59;print$/}0..20
    Tom Melly, pm@tomandlu.co.uk
Re: How to do tables
by EvanK (Chaplain) on Jan 23, 2007 at 15:19 UTC
    first I would advise to take a look at the (current) CPAN docs for CGI. You might find particularly useful the "CREATING STANDARD HTML ELEMENTS" section, as it provides this example:
    print table({-border=>undef}, caption('When Should You Eat Your Vegetables?'), Tr({-align=>CENTER,-valign=>TOP}, [ th(['Vegetable', 'Breakfast','Lunch','Dinner']), td(['Tomatoes' , 'no', 'yes', 'yes']), td(['Broccoli' , 'no', 'no', 'yes']), td(['Onions' , 'yes','yes', 'yes']) ] ) );
    Update: A much better monk already beat me to the punch, and with a more detailed response to boot :)

    __________
    The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.
    - Terry Pratchett

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: How to do tables
by Rhandom (Curate) on Jan 23, 2007 at 17:18 UTC
    My post is based out of curiosity.

    There are three camps (at least):
    1. Those that hard code their html inline in their perl.
    2. Those that use CGI.pm to generate their html.
    3. Those that use a templating system to fill values into an existing html template.
    All three choices have their benefits. They also each have downsides. These choices all cover a scale that has raw execution speed on one end and good design (separation of content) on the other end. Since the HTML generation aspects of CGI.pm fall in the middle of this scale, I am always interested to know what factors are driving the decision to use CGI.pm over a templating system or just raw html.

    So that was a long way to ask: why have you chosen to use CGI.pm over either raw html or a templating system.

    If the answer is "I'm not sure" then a would suggest using a templating system - even if it means that the html template is inline as a variable in the code.

    my @a=qw(random brilliant braindead); print $a[rand(@a)];

      One thing I've done a few times (when not using Mason (or recently, other not-Perl not-CGI mechanisms)) is to let the TT source ride along after the __END__ marker of the CGI's source. Then you just my $tmpl_text = do { local $/; <DATA> }; to slurp it in before handing it to $tmpl->process( \$tmpl_text  );

      You could also get fancy with Inline::File and store separate templates after separate markers, but at that point you're probably approaching being better off storing them elsewhere as separate files.

        The CGI::Ex::App framework which I use allows you to either return a file name or a reference to a scalar containing the template. It then passes the value along to CGI::Ex::Template which is Template Toolkit compatible and will process either a filename or a reference to a scalar.

        This allows for easy prototyping in a single file, and then the capability to move items out to separate files later.

        sub main_file_print { 'my_content/my_script/main.html' } sub otherstep_file_print { return \ qq{<html> My template follows here. </html> }; }


        my @a=qw(random brilliant braindead); print $a[rand(@a)];