I have a multidimensional hash. Sometimes, when there is only one hash, then I want to send that single hash to a specific HTML::Template file.
I've read that you can combine two hashes like this:
@hash1{ keys %hash2 } = values %hash2;
But in my scenario I would like all the keys and values in my hash to be sent to the template file.
What I want to avoid is this:
$template->param(
param1 => $myhash{'param1'},
param2 => $myhash('param2'),
paramX => $myhash('paramX'),
name => "Bob",
);
I want something like this:
$template->param(
{ keys %myhash } = values %myhash,
name => "Bob"
);
But I guess that would not work.
Any ideas?
Edit. This code returns error HTML::Template->param() : You gave me an odd number of parameters to param()!
my %myhash = ( '9343X' => { 'param1' => 'Walter', 'param2' => 'Blue', 'param3' => 513 } );
$items = scalar (keys %myhash);
if ($items == 1) {
my ($firstkey) = %myhash;
$template->param(
$myhash{$firstkey},
name => "Bob"
);
}
However, if I replace $myhash{$firstkey}
with $param1 = $myhash{$firstkey}{'param1'}
it works fine. But I want to avoid specifying each parameter.
CodePudding user response:
A hash in list context produces a list of paired key-value scalars.
$template->param(
%myhash,
name => "Bob"
);