배열을 인스턴스 변수로 저장하는 객체가 있습니다. Perl은 이것을 지원하지 않는 것 같기 때문에 대신 배열에 대한 참조를 저장해야합니다. 그러나 일단 배열이 만들어지면이 배열을 어떻게 변이 시킬지 알 수 없습니다. 메서드는 로컬 복사본 만 변경하는 것처럼 보입니다. (현재 addOwnedFile()의 끝에서 객체 데이터는 변경되지 않습니다.Perl의 배열에 대한 접근 자 메서드
sub new {
my ($class) = @_;
my @owned_files =();
my @shared_files =();
my $self = {
#$[0] is the class
_name => $_[1],
_owned_files => \[],
_shared_files => \[],
};
bless $self, $class;
return $self;
}
#Add a file to the list of files that a user owns
sub addOwnedFile {
my ($self, $file) = @_;
my $ref = $self -> {_owned_files};
my @array = @$ref;
push(@array, $file);
push(@array, "something");
push(@{$self->{_owned_files}}, "something else");
$self->{_owned_files} = \@array;
}
[perldoc perlref] (http://perldoc.perl.org/perlref.html) 및 [perldoc perlreftut] (http://perldoc.perl.org/perlreftut.html)을 읽으십시오. –