0

I'm new to perl CGI and trying to print hash data to html using perl CGI. Below is my code.

#!/usr/bin/perl

use strict;
use warnings;
use CGI ':standard';
use CGI::Carp qw(fatalsToBrowser warningsToBrowser);

our %dashboard_data;
my $q = new CGI;

################################################################################
## Read the dashboard_data file
require "partition_tsv_dashboard.pl";

print   $q->header,
        $q->start_html('Top Level TSV Dashboards'),
        $q->h1('Top Level TSV Dashboards'),
foreach my $model (sort keys %{$dashboard_data{'project'}}){
        $q->h2("Model: $model"),
}
        $q->end_html;

The partition_tsv_dashboard.pl is the nested hash. Contains below data.

#!/usr/bin/perl

$dashboard_data {'project'} {'model1'} {'P00'} {'TSV-193'} {'COMP_0_P00'} = '';
$dashboard_data {'project'} {'model2'} {'P00'} {'TSV-001'} {'COMP_0_P00'} = '';
$dashboard_data {'project'} {'model3'} {'P00'} {'TSV-001'} {'COMP_0_P00'} = '';
$dashboard_data {'project'} {'model4'} {'P00'} {'TSV-001'} {'COMP_0_P00'} = '';
$dashboard_data {'project'} {'model5'} {'P00'} {'TSV-001'} {'COMP_0_P00'} = '';

1;

I'm getting below error when I try execute the CGI program. How do I map the a hash values to CGI object? Can someone help me with this.

Output.

Software error:
syntax error at /web/keystone/cgi-bin/tsv_dashboard.pl line 26, near "$model ("
Execution of /web/keystone/cgi-bin/tsv_dashboard.pl aborted due to compilation errors.
1
  • 1
    I don't think you can have a for loop within another list. Try split the print statement up instead Commented Sep 12, 2021 at 20:50

2 Answers 2

2

You cannot have a for loop inside the argument list of the print statement. Instead try split it up like this.

print   $q->header,
        $q->start_html('Top Level TSV Dashboards'),
        $q->h1('Top Level TSV Dashboards');

foreach my $model (sort keys %{$dashboard_data{'project'}}){
        print $q->h2("Model: $model");
}
print   $q->end_html;
Sign up to request clarification or add additional context in comments.

3 Comments

HTML injection bug in the h2 line
Thanks for the comment. It would depend on what the script partition_tsv_dashboard.pl does right? Since it is responsible for populating the %dashboard_data hash. If the script does not use user input to populate the hash, it cannot be the source of a HTML injection bug as I understand it..
I find it very unlikely that %dashboard_data contains HTML rather than plain text.
1

The prior answer from @Håkon Hægland is likely the best answer, but you could use map to achieve a similar result.

use CGI ':standard';

my $q = new CGI;

my @a = ("blah", "wooble");

print $q->h1("things"), "\n",
      (map { $q->h2($_) } @a), "\n",
      $q->br, "\n";

To get this output:

% perl test.pl
<h1>things</h1>
<h2>blah</h2><h2>wooble</h2>
<br />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.