I want to decode all the HTML entities in a complicated data structure. Basically I'm looking for a "super map()" function. Here's what I have so far:
sub _html_decode {
my $self = shift;
my $ref = shift;
if (ref($ref) eq "HASH") {
$self->_html_decode_hash($ref)
}
if (ref($ref) eq "ARRAY") {
$self->_html_decode_array($ref);
}
}
sub _html_decode_array {
my $self = shift;
my $ref = shift;
unless (@$ref) {return;}
foreach (0 .. (scalar(@$ref) - 1)) {
if (ref($ref->[$_]) eq "HASH") {
$self->_html_decode_hash($ref->[$_]);
}
if (ref($ref->[$_]) eq "ARRAY") {
$self->_html_decode_array($ref->[$_]);
}
else {
$ref->[$_] = decode_entities($ref->[$_]);
}
}
}
sub _html_decode_hash {
my $self = shift;
my $ref = shift;
unless (%$ref) {return;}
while (my ($k, $v) = each %$ref) {
if (ref($v) eq "HASH") {
$self->_html_decode_hash($v);
}
if (ref($v) eq "ARRAY") {
$self->_html_decode_array($v)
}
else {
$ref->{$k} = decode_entities($v);
}
}
}