Trying to create a nested hash in Perl that is populated with entries from a CGI form.
Here is a snippet:
my $section = $q->param('section') || undef;
my %data;
if($section) {
my $prod1part = $q->param('prod1part') || undef;
my $prod2part = $q->param('prod2part') || undef;
my $prod3part = $q->param('prod3part') || undef;
my $prod1name = $q->param('prod1name') || undef;
my $prod2name = $q->param('prod2name') || undef;
my $prod3name = $q->param('prod3name') || undef;
my $prod1price = $q->param('prod1price') || undef;
my $prod2price = $q->param('prod2price') || undef;
my $prod3price = $q->param('prod3price') || undef;
my $dealprice = $q->param('dealprice') || undef;
my $dealtype = $q->param('dealtype') || undef;
my $id = &generateID();
#GENERATE DATA STRUCTURE FOR PDF
$data = { product1 => { part => $prod1part,
name => $prod1name,
price => $prod1price,
},
product2 => { part => $prod2part,
name => $prod2name,
price => $prod2price,
},
product3 => { part => $prod3part,
name => $prod3name,
price => $prod3price,
},
... and so on ...
};
}
But then when I attempt to dump the data structure to check it:
print $q->header(-type=>'text/plain');
print Data::Dumper->new([\%data],[qw/data/])->Indent(3)->Quotekeys(0)->Dump;
All I get is an empty data structure!!!
$data = {};
I’m obviously doing something wrong, but I can’t figure out what… Ideas?
What you have:
What you probably meant:
That being said, you are repeating yourself quite a lot. Try this instead: