如何在深入Hoh中访问一个不存在的密钥作为Moose属性时如何死?

时间:2010-11-12 10:15:21

标签: perl moose

我有一个Moose对象,其属性是hash:

has 'custom_fields' => (
    traits  => [qw( Hash )],
    isa     => 'HashRef',
    builder => '_build_custom_fields',
    handles => {
        custom_field        => 'accessor',
        has_custom_field    => 'exists',
        custom_fields       => 'keys',
        has_custom_fields   => 'count',
        delete_custom_field => 'delete',
    },
);

around 'custom_field' => sub {
    my $orig  = shift // confess;
    my $self  = shift // confess;
    my $field = shift // confess;

    confess "Attempt accessing non-existing custom field '$field'"
        unless ( @_ or $self->has_custom_field($field) );

    $self->$orig( $field, @_ );
};

他的作品很简单,一级哈希。现在,我想允许深度哈希(散列哈希哈希值),并且只要尝试访问不存在的(可能很深的)密钥,就会confess

更新 也许以某种方式使用Data::Diver

2 个答案:

答案 0 :(得分:1)

一般来说,如果你想要以面向对象的方式处理复杂的数据结构,你应该把数据结构变成一个对象树。对于穆斯强制,这也可以相当透明地模拟。

答案 1 :(得分:0)

您可以通过autovivification查看关闭自动审核。

您可以传递使其死亡的unimport例程'exception',它可能已经自动存档。

相关问题