4

通过自学 Moose,我正在研究与特定硬件接口的 Moose 对象。所述硬件采用许多不同的命令来设置硬件的各种属性,所有形式PROPERTYNAME=VALUE的设置器和PROPERTYNAME?获取器(请注意,这些“设置器”和“获取器”位于硬件的网络接口上)。我想要做的是创建一个对象,其中硬件的所有这些属性都使用类似属性的接口来实现。由于获取和设置各种属性对所有属性都采用相同的形式,有没有办法从这些属性的列表中自动生成 setter 和 getter?

IE:而不是这样:

Package MyHardware;
use Moose;
has property1 => (
    'is' => 'rw',
    'reader' => 'set_property1',
    'writer' => 'get_property1',
);

has property2 => (
    'is' => 'rw',
    'reader' => 'set_property2',
    'writer' => 'get_property2',
);

# ...

has propertyN => (
    'is' => 'rw',
    'reader' => 'set_propertyN',
    'writer' => 'get_propertyN',
);

有什么我可以这样做的:

Package MyHardware;
use Moose;

attributes => (
    'is' => 'rw',
    'names' => [qw/property1 property2 ... propertyN/],
    'reader' => sub {
        my $self = shift;
        my $property = shift;
        return $self->_send_command("$property?");
    },
    'writer' => sub {
        my $self = shift;
        my $property = shift;
        my $value = shift;
        return $self->_send_command("$property=$value");
    },
);

编辑:这就是我想要发生的事情:

# CALLER:
my $hw = MyHardware->new();
$hw->property1('foo');
print $hw->property2 . "\n";

和“引擎盖下”:

$hw->property1('foo');
# Becomes 
sub { return $hw->_send_command('property1=foo'); }

# And

$hw->property2();
# Becomes
sub { return $hw->_send_command('property2?'); }
4

5 回答 5

5

循环遍历属性怎么样?

use strict;
use warnings;

use Moose;

foreach my $prop ( qw( property1 property2 property3 property4 ) ) { 
    has $prop => (
        is => 'rw',
        isa => 'Str',
        reader => "get_$prop",
        writer => "set_$prop",
    );  
}

1;
于 2013-01-28T21:15:37.657 回答
2

弄清楚了。我意识到我根本不应该使用属性来做到这一点。相反,我将使用Class::MOP::Class动态生成方法,如下所示:

my $meta = Class::MOP::Class->initialize(__PACKAGE__);
foreach my $prop (qw/property1 property2 property3/) {
    $meta->add_method(qq/set_$prop/, sub { 
            my $self = shift;
            my $value = shift;
            return $self->_send_command(qq/$prop=$value/);
        }
    );
    $meta->add_method(qq/get_$prop/, sub { 
            my $self = shift;
            return $self->_send_command(qq/$prop?/);
        }
    );
}

调用 has() 会有效地将对象状态放在两个地方——硬件上和实例中——我只希望它在一个地方。

于 2013-01-29T17:32:18.903 回答
2

您不存储任何值,因此您不需要属性。

您甚至不需要两个 sub,因为您需要一个用于获取和设置的名称。

for my $prop (qw( property1 property2 property3  )) { 
   my $accessor = sub {
      my $self = shift;
      if (@_) {
         $self->_send_command("$prop=$value");
      } else {
         return $self->_send_command("$prop?");
      }
   };

   no strict 'refs';
   *$prop = $accessor;
}
于 2013-01-29T01:56:18.510 回答
1

我建议为您的每个属性使用 has 而不是单独的属性。

Package MyHardware;
use Moose;
has properties => (
'is' => 'rw',
'isa' => 'HashRef',
'lazy_build' => 1,
);

sub _build_properties {
    my $self = shift;
    return {
        'property1' => '',
        'property2' => '',
    };
}

print $self->properties->{property1};
于 2013-01-28T23:56:31.597 回答
1

为实例数据生成 getter 和 setter

BEGIN 
{
    my @attr = qw(prop1 prop2 prop3 prop4);
    no strict 'refs';
    for my $a (@attr)
    {
        *{__PACKAGE__ . "::get_$a"} = sub { $_[0]->{$a}         };
        *{__PACKAGE__ . "::set_$a"} = sub { $_[0]->{$a} = $_[1] };
    }
}

于 2013-03-15T19:27:37.040 回答