I.E.:而不是:
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();# Becomessub { return $hw->_send_command('property2?'); }解决方法 循环遍历属性怎么样?
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;总结
以上是内存溢出为你收集整理的perl – 如何在Moose中自动生成绑定到网络服务的一堆setter / getter?全部内容,希望文章能够帮你解决perl – 如何在Moose中自动生成绑定到网络服务的一堆setter / getter?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)