in reply to Table in Perl CGI

What‽ No love for jeffa's new Spreadsheet::HTML

#!/usr/bin/perl -w use strict; use Spreadsheet::HTML; my %hash = ( kumquat => 'orange', pomegranate => 'red', cherimoya => 'green', ling +onberry => 'purple' ); my $data = [ [sort keys %hash], [sort { $a cmp $b } values %hash] ]; my $generator = Spreadsheet::HTML->new( data => $data, matrix => 1, # no <th> indent => ' ' ); print $generator->portrait; __END__

Output:

<table> <tr> <td>cherimoya</td> <td>kumquat</td> <td>lingonberry</td> <td>pomegranate</td> </tr> <tr> <td>green</td> <td>orange</td> <td>purple</td> <td>red</td> </tr> </table>

Now if there was just a way to make a callback sub that would color the table cells with the fruit colors ...

#!/usr/bin/perl -w use strict; use Spreadsheet::HTML; my %hash = ( kumquat => 'orange', pomegranate => 'red', cherimoya => 'green', lin +gonberry => 'purple' ); my ($fruits, $colors) = ( [sort keys %hash], [sort { $a cmp $b } value +s %hash] ); my $data = [ $fruits, $colors ]; my $generator = Spreadsheet::HTML->new( data => $data, indent => ' ', th => { style => { background => $colors } }, td => [ sub { my $c = shift; $c =~ s!(.*)!<font color="$1">$1</f +ont>!; return $c } ], ); print $generator->portrait; __END__

Output:

<table> <tr> <th style="background: green">cherimoya</th> <th style="background: orange">kumquat</th> <th style="background: purple">lingonberry</th> <th style="background: red">pomegranate</th> </tr> <tr> <td><font color="green">green</font></td> <td><font color="orange">orange</font></td> <td><font color="purple">purple</font></td> <td><font color="red">red</font></td> </tr> </table>

Yep, I know this is deprecated HTML; just playing :-)

The way forward always starts with a minimal test.

Replies are listed 'Best First'.
Re^2: Table in Perl CGI
by jeffa (Bishop) on Aug 03, 2015 at 21:24 UTC

    Thank you for checking out my module, this really is an honor. :) You can simplify a few things, for what it is worth:

    use strict; use warnings; use Spreadsheet::HTML qw( portrait ); my %hash = ( kumquat => 'orange', pomegranate => 'red', cherimoya => 'green', lingonberry => 'purple' ); my @data = ( [sort keys %hash], [map $hash{$_}, sort keys %hash] ); print portrait( data => \@data, indent => ' ', th => { style => { background => $data[1] } }, td => { style => { background => $data[1] } }, ); __DATA__ <table> <tr> <th style="background: green">cherimoya</th> <th style="background: orange">kumquat</th> <th style="background: purple">lingonberry</th> <th style="background: red">pomegranate</th> </tr> <tr> <td style="background: green">green</td> <td style="background: orange">orange</td> <td style="background: purple">purple</td> <td style="background: red">red</td> </tr> </table>

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    

      It's working nicely ... I wrote the post above after simply testing it out in response to the OP; I'm using it now to build some nightly reports. I particularly like how easy it is to pass attributes to the tag builder. You're right about simplifying, I haven't needed to use a callback at all (but it's nice to know it's built-in for when it's needed).

      The way forward always starts with a minimal test.
Re^2: Table in Perl CGI
by alokranjan (Acolyte) on Aug 03, 2015 at 18:50 UTC
    Many thanks for the suggestion.

    Can you please let me know if I can use "if" condition with the value of my hash and then decide the color of the cell in the table?

    Thanks
    Alok