如何在Moose中自动生成绑定到网络服务的一堆setter / getter?

时间:2013-01-28 20:48:18

标签: perl moose

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

I.E。:而不是:

PROPERTYNAME?

我能做到这样吗:

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";

5 个答案:

答案 0 :(得分: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;

答案 1 :(得分:2)

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

你甚至不想要两个潜艇,因为你想要一个名字来获取和设置。

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

答案 2 :(得分: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()来实现它会有效地将对象状态放在两个地方 - 硬件和实例中 - 我只想把它放在一个。

答案 3 :(得分: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};

答案 4 :(得分: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] };
    }
}