Still learning how to do perl with plack. I have a simple app to post check boxes and other form elements. I would like to loop through check box name and value and place them in a new hash. These are obtained through a post request in Plack::Request body_parameters. Where am stuck is how to loop through the Plack::Request body_parameters. Dumper body_parameters as follows:
$VAR1 = bless( {
'c_xcode' => 'J23-H0P',
'c_ycode' => 'G12-Y4T',
'c_zcode' => 'Q87-S7B',
's_xlist' => 'T66-Y9A',
's_ylist' => 'A43-P9W',
's_zlist' => 'T71-K2L'
}, 'Hash::MultiValue' );
What I would like is to convert body_parameters into a hash like this:
%form_hash = (
'c_xcode' => 'J23-H0P',
'c_ycode' => 'G12-Y4T',
'c_zcode' => 'Q87-S7B',
's_xlist' => 'T66-Y9A',
's_ylist' => 'A43-P9W',
's_zlist' => 'T71-K2L'
);
What is the best way to do this? Here's what I've tried, which I got close - it prints my desired hash to dumper, but I can't pass the \%form_hash to the foreach loop below:
my $form = $posted->body_parameters;
my @k = keys %$form;
my @v = values %$form;
@form_hash{@k} = @v;
# Dumper ( \%form_hash) works, but can't pass this to foreach;
# "Experimental keys on scalar is now forbidden" when trying on
# foreach my $key (sort(keys( \%form_hash ))) below.
Here is the complete snippet
#!/usr/bin/perl
use strict 'refs';
use Plack::Request;
use Data::Dumper;
my $app = sub {
my $env = shift;
my $form = Plack::Request->new($env);
print Dumper ( $form->body_parameters ) ;
#how to convert $form->body_parameters to new %form_hash?
my %form_hash;
my %cb_hash;
foreach my $key (sort(keys( %form_hash ))) {
if ( substr($key,0,2) eq "c_" ){
my $cb_key = $key;
my $cb_val = $form_hash{$key};
$cb_hash{$cb_key} = $cb_val;
}
}
};