Trying to use map and grep to figure this out, any idea whats wrong? I keep getting a
Can’t use string (“10”) as a HASH ref while “strict refs” error when I try to print the values of the new hash
sub scrub_hash{
my($self,$hash_ref) = @_;
my $scrubbed_hash = map { defined $hash_ref->{$_} ? ($_ => $hash_ref->{$_}) : () } keys %{$hash_ref};
print STDERR "[scrub]". $_."\n" for values %{$scrubbed_hash};
}
used here
…
my $params_hash = $cgi->Vars();
my $scrubbed = $self->scrub_empty_params($params_hash) if $self->is_hash($params_hash);
in this case the params that are undefined when a form is submitted via post still show up as key1=&key2= so scrub takes em off
In this line here:
You are assigning a list from
mapinto the scalar$scrubbed_hash.mapin scalar context will return the number of elements in the list (10).Rather than assigning to a scalar, assign to a plural hash:
If you really wanted to use a scalar for
$scrubbed_hashyou will need to wrap the map statement with{map {...} args}which will construct an anonymous hash out of the list.To filter out the elements in place, you could use the
deletefunction:per the update:
The
scrub_empty_paramsmethod should look something like this:If that is not working for you, then it may be that your values are defined, but have a length of 0.
You might want to remove a bit of boiler plate from your API by creating a different
->Vars()method that returns a filtered hash: