我有一個將數組存儲爲實例變量的對象。由於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) – 2011-03-31 02:15:25