Class::ArrayObjects을 사용하는 간단한 프로그램을 작성했지만 예상대로 작동하지 않았습니다. 이 프로그램은 다음과 같습니다Class :: ArrayObjects는 어떻게 사용합니까?
TestArrayObject.pm :
package TestArrayObject;
use Class::ArrayObjects define => {
fields => [qw(name id address)],
};
sub new {
my ($class) = @_;
my $self = [];
bless $self, $class;
$self->[name] = '';
$self->[id] = '';
$self->[address] = '';
return $self;
}
1;
Test.pl
use TestArrayObject;
use Data::Dumper;
my $test = new TestArrayObject;
$test->[name] = 'Minh';
$test->[id] = '123456';
$test->[address] = 'HN';
print Dumper $test;
내가 Test.pl를 실행하면 출력 데이터는 다음과 같습니다
$VAR1 = bless([
'HN',
'',
''
], 'TestArrayObject');
I을 'name'과 'id'에 대한 내 데이터는 어디에 있습니까?
감사합니다. 민.
메소드 생성기'BEGIN {(qw (name id address))에 대해 {엄격한 'refs가 없습니다.';}를 작성할 때 일부 코드 행과 관리를 저장할 수 있습니다. my $ no = &$_; * {ucfirst()} = sub {my $ self = shift; $ self -> [$ no] = 시프트 @@; return $ self -> [$ no];};}}' –
어쨌든 get/set을 싫어하고 get과 set을 분리하여 사용하는 것이 좋습니다. 'BEGIN {for (qw (name id address)) {엄격한 'refs'없음; 내 $ 아니오 = &$_; 내 $ 이름 = ucfirst; * { "get $ name"} = sub {shift -> [$ no]}; –
예, get/set 사용에 관해서는 동의합니다. * { "set $ name"} = sub {$ _ [0] -> [$ no] = $ _ [1]};}} 그것은 코드를 더 많은 것으로 만든다. –