如何灵活地向Moose对象添加数据?

Dav*_*d B 7 perl moose

我正在为驼鹿物体写一个模块.我想允许使用此对象的用户(或我自己......)根据需要随时添加一些字段.我无法先验地定义这些字段,因为我根本不知道它们会是什么.

我目前只是添加了一个名为extra的hashref类型的字段rw,因此用户可以简单地将东西放入该哈希:

# $obj is a ref to my Moose object    
$obj->extra()->{new_thingie}="abc123"; # adds some arbitrary stuff to the object
say $obj->extra()->{new_thingie};
Run Code Online (Sandbox Code Playgroud)

这有效.但是......这是一种常见的做法吗?还有其他(可能更优雅)的想法吗?

注意我不想创建另一个扩展这个模块的模块,这实际上只是为了我想添加的即时内容.

pha*_*lon 6

我可能会通过本机特征来做到这一点:

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',
    },
);

sub _build_custom_fields { {} }
Run Code Online (Sandbox Code Playgroud)

在对象上,您将使用以下内容:

my $val = $obj->custom_field('foo');           # get field value
$obj->custom_field('foo', 23);                 # set field to value

$obj->has_custom_field('foo');                 # does a specific field exist?
$obj->has_custom_fields;                       # are there any fields?

my @names = $obj->custom_fields;               # what fields are there?
my $value = $obj->delete_custom_field('foo');  # remove field value
Run Code Online (Sandbox Code Playgroud)

像这样的东西的一个常见用例是向异常和消息类添加可选的内省数据.


Sin*_*nür 5

如果你还没有使类不可变(除了我对动态更改类定义的担忧之外,还有不执行此操作的性能损失),您应该能够通过获取对象的元类来实现这一点(使用$meta = $object->meta),并使用add_attribute在方法类:: MOP ::类.

#!/usr/bin/perl

package My::Class;

use Moose;
use namespace::autoclean;

package main;

my $x = My::Class->new;
my $meta = $x->meta;
$meta->add_attribute(
    foo => (
        accessor => 'foo',
    )
);

$x->foo(42);

print $x->foo, "\n";

my $y = My::Class->new({ foo => 5 });
print $y->foo, "\n";
Run Code Online (Sandbox Code Playgroud)

输出:

42
5