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

Hallo to everyone

I treid to make cgi table in my newindex.pm to return data from hashref in a table in my test.pl but cant.

I will appreciate when you give me some ideas or guide me. im new in perl! i also search alot and i know how to use cgi table in "test.pl" but not in "newindex.pm"

Here is my code

package newindex; use strict; sub getTitle { return "workers"; } sub getContent { my $cgi = shift; $hashref = { 'Adam' => 'Eve', 'Clyde' => 'Bonnie', }; my $table = ; return $table; } 1;

test.pl

#!"C:\xampp\perl\bin\perl.exe" use strict; use warnings; use CGI qw/:standard/; push @INC, "."; sub getHeaders { my $cgi = shift; return $cgi->header('text/html'); } sub getBody { my $cgi = shift; return $cgi->start_html( getContent($cgi,'title') ) . getContent($cg +i,'content') . $cgi->end_html; } sub getModule { my $cgi = shift; my $action = $cgi->param('action') // 'newindex'; $action = 'newindex' unless (-f $action.'.pm'); eval "use $action;"; return $action; } sub getContent { my ($cgi,$call) = @_; my $module = getModule($cgi); my $caller = 'get'.ucfirst($call); return $module->$caller($cgi); } sub main { my $cgi = new CGI; print getHeaders($cgi) . getBody($cgi); } main();

Replies are listed 'Best First'.
Re: How to use cgi table in index.pm perl
by haukex (Archbishop) on Dec 13, 2017 at 11:03 UTC

    Have a look at a templating engine like Template::Toolkit, or perhaps HTML::Tiny:

    use warnings; use strict; use HTML::Tiny; my $hashref = { 'Adam' => 'Eve', 'Clyde' => 'Bonnie', }; my $h = HTML::Tiny->new; print $h->table( [ $h->tr( [ $h->th( 'Foo', 'Bar' ) ], map( { [ $h->td( $_, $hashref->{$_} ) ] } sort keys %$hashref ) ) ] ); __END__ <table><tr><th>Foo</th><th>Bar</th></tr> <tr><td>Adam</td><td>Eve</td></tr> <tr><td>Clyde</td><td>Bonnie</td></tr> </table>

      Thank you. but i cant use print in index.pm

        i cant use print in index.pm

        $h->table(...) returns a string, which you can save in a variable and return from a function like your sub getContent.

Re: How to use cgi table in newindex.pm perl
by 1nickt (Canon) on Dec 13, 2017 at 13:03 UTC

    You could use Spreadsheet::HTML by jeffa:

    perl -E' use strict; use warnings; use Data::Dumper; use Spreadsheet::HTML; my $href = { Adam => "Eve", Clyde => "Bonnie" }; my @data = ( [qw/Man Woman/], map { [$_, $href->{ $_ }] } keys %{ $hre +f } ); my $make = Spreadsheet::HTML->new( data => \@data ); my $html = $make->portrait; say $html; ' <table><tr><th>Man</th><th>Woman</th></tr><tr><td>Adam</td><td>Eve</td +></tr><tr><td>Clyde</td><td>Bonnie</td></tr></table>

    But honestly your best bet is to use a web application framework like Dancer2, and have your route return JSON to a frontend table builder, e.g. DataTables.

    Hope this helps!



    The way forward always starts with a minimal test.
Re: How to use cgi table in newindex.pm perl
by marto (Cardinal) on Dec 13, 2017 at 12:24 UTC

      Thank You . Im really confused with cgi table in newindex.pm

        It does look like you're over complicating things. I stand by my suggestion of Mojolicious::Lite. Make life easy for yourself and learn the basics. The documentation is really good IMHO.

Re: How to use cgi table in index.pm perl
by poj (Abbot) on Dec 13, 2017 at 11:15 UTC

    Can you post the code for test.pl ?

    poj

      sure! should i post it in my question or in replay ?

        should i post it in my question or in replay ?

        That is your choice, in this case I'd suggest editing the root node to have everything in one place - but if you edit a node, please make sure to clearly mark your updates as such. See How do I change/delete my post? (I'd also suggest removing all those blank lines from the root node while you're at it.)

        Just add to the end of your original post