alokranjan has asked for the wisdom of the Perl Monks concerning the following question:
Can anyone please let me know how can I print the values of hash in tabular form in Pel CGI?
Below is my code, which is not showing the table:
Any help/assistance would be highly appreciated.print $cgi->header(), $cgi->start_html("Fruits and colors"), $cgi->table({-border=>1}, $cgi->Tr($cgi->th([" Fruit"," Color"])), for my $f (keys %tbl){ map { $cgi->Tr($cgi->td([$f,$tbl{$f}])) } sort keys %tbl } ), $cgi->end_html();
Thanks
Alok
|
|---|
| Replies are listed 'Best First'. | |||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Re: Table in Perl CGI
by GrandFather (Saint) on Aug 02, 2015 at 09:54 UTC | |||||||||||||||||
As a general thing mixing HTML and code is a Bad Thing™ because it makes it harder to maintain both the code and the HTML. Better is to use modules such as HTML::Template which lets you write most of the page as an ordinary HTML file and add a little magic to allow parts of the HTML to be provided by the script. Consider:
Prints:
Usually an external file would be used instead of the here doc and string in the sample. Update: output fixed - thanks CountZero
Premature optimization is the root of all job security
| [reply] [d/l] [select] | ||||||||||||||||
|
Re: Table in Perl CGI
by CountZero (Bishop) on Aug 02, 2015 at 12:30 UTC | |||||||||||||||||
Output:
Update: Added missing closing </table> tag. Thank you beech CountZero A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James My blog: Imperial Deltronics | [reply] [d/l] [select] | ||||||||||||||||
|
Re: Table in Perl CGI
by 1nickt (Canon) on Aug 02, 2015 at 15:57 UTC | |||||||||||||||||
What‽ No love for jeffa's new Spreadsheet::HTML‽
Output:
Now if there was just a way to make a callback sub that would color the table cells with the fruit colors ...
Output:
Yep, I know this is deprecated HTML; just playing :-)
The way forward always starts with a minimal test.
| [reply] [d/l] [select] | ||||||||||||||||
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:
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) | [reply] [d/l] [select] | ||||||||||||||||
by 1nickt (Canon) on Aug 06, 2015 at 20:25 UTC | |||||||||||||||||
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.
| [reply] [d/l] | ||||||||||||||||
by alokranjan (Acolyte) on Aug 03, 2015 at 18:50 UTC | |||||||||||||||||
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? ThanksAlok | [reply] | ||||||||||||||||
|
Re: Table in Perl CGI
by pme (Monsignor) on Aug 02, 2015 at 09:27 UTC | |||||||||||||||||
This is one of the ways.
| [reply] [d/l] | ||||||||||||||||
|
Re: Table in Perl CGI
by i5513 (Pilgrim) on Aug 02, 2015 at 09:41 UTC | |||||||||||||||||
Cgi module has been Regards, Updated after read Re^2: Table in Perl CGI Updated with an example after read Re^2: Table in Perl CGI, i think it is better to know about TT before than after. | [reply] [d/l] | ||||||||||||||||
by ww (Archbishop) on Aug 02, 2015 at 13:58 UTC | |||||||||||||||||
I upvoted the parent for the generally good advice, but was sorely tempted to downvote for the intial, erroneous (though not quite FUD) statment. See also GrandFather's far more detailed (and nuanced) discussion at Re: Table in Perl CGI. http://search.cpan.org/dist/perl-5.20.0/pod/perldelta.pod#Module_removals Module removals The following modules will be removed from the core distribution in a future release, and will at that time need to be installed from CPAN. Distributions on CPAN which require these modules will need to list them as prerequisites. The core versions of these modules will now issue "deprecated"-category warnings to alert you to this fact. To silence these deprecation warnings, install the modules in question from CPAN. Note that the planned removal of these modules from core does not reflect a judgement about the quality of the code and should not be taken as a suggestion that their use be halted. Their disinclusion from core primarily hinges on their necessity to bootstrapping a fully functional, CPAN-capable Perl installation, not on concerns over their design. CGI and its associated CGI:: packages .... The 5.22 perldelta contains a much abbreviated announcement, simply stating that CGI.pm has been removed from core. Some individuals chose to use the word "deprecated" but in fact are expressing personal opinion rather than a concensus. The key phrase is "removed from core;" but not "officialy deprecated" as your statement might lead a reader to believe. Some CGI methods are outdated. | [reply] | ||||||||||||||||
by 1nickt (Canon) on Aug 02, 2015 at 15:19 UTC | |||||||||||||||||
| [reply] [d/l] | |||||||||||||||||