1

我想解码复杂数据结构中的所有 HTML 实体。基本上我正在寻找一个“超级地图()”功能。这是我到目前为止所拥有的:

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);
        }
    }
}
4

3 回答 3

3

我认为这应该可以,但我还没有测试过。

sub _html_decode {
    my ($self, $ref) = @_;

    if (ref($ref) eq "HASH") {
        for my $value (values %{$ref}) {
            $self->_html_decode($value);
        }
    }
    elsif (ref($ref) eq "ARRAY") {
        for my $value (@{$ref}) {
            $self->_html_decode($value);
        }
    }
    else {
        $_[1] = decode_entities($_[1]);
    }
}

我承认最后一部分并不漂亮。

于 2008-12-17T17:31:48.020 回答
3

要对任意复杂数据结构的每个元素执行子例程,请查看访问者设计模式。基本上,您的数据结构是一个对象,它知道它仍需要处理哪些元素,并将您的子例程应用于它们。那里还有一些迭代器模式,因为你知道如何

我的Netscape::Bookmarks模块中有一个示例。该数据结构与几种不同类型的对象深度嵌套。通过使用访问者模式,大部分复杂性都消失了。

除此之外,您可能还想查看我的Object::Iterate模块。它具有imap使用对象而不是列表的功能。我__next__从 Python 中借鉴了 的想法并将其应用到 Perl。

于 2008-12-17T19:45:00.560 回答
2

Data::Rmap似乎也这样做了。有没有人有这个模块的经验?

于 2008-12-18T01:01:15.663 回答