in reply to Table in Perl CGI

Hello,

Cgi module has been deprecated removed from core, so I think it is better to learn about Template toolkit, see how to use hash on TT

See next code to see how to work with your example:
#!/usr/bin/perl -w use strict; use Template; # Define hash my %fruits= ( "naranja" => "naranja" , "limón" => "amarillo" ); # Simulate a file (this content should be separated normally, on a fil +e) my $html = << 'EOT'; <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>fruits and colors</title> </head> <body> <table border="1"> <tr> <th>Fruit</th> <th>Color</th> </tr> [%# The end string "-%" will remove empty lines from source code -%] [% FOREACH fruit IN fruits.keys -%] <tr> <td> [% fruit -%] </td> <td> [% fruits.$fruit -%] </td> </tr> [% END -%] </table> </body> </html> EOT # Create the template var: my $template=Template->new(); # Process the template, telling that fruits var inside the template is + %fruits on perl code # and that $html has the template text: $template -> process (\$html,{ fruits => \%fruits }) || die $template- +>error(); # Read man Template to know more about this module
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.

Replies are listed 'Best First'.
Re^2: Table in Perl CGI
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.

Re^2: Table in Perl CGI
by 1nickt (Canon) on Aug 02, 2015 at 15:19 UTC

    Not only is CGI not deprecated, but Template would be a terrible choice for the OP and his problem. It is not for beginners learning their first perl to HTML script.

    (I am a regular user and proponent of TT2, BTW).

    The way forward always starts with a minimal test.