-1
我試了幾個小時纔將子數組存儲到一個對象中並失敗。也許你的某個人可以告訴我如何用perl存儲深層副本。 sry我不知道這個問題是否清楚,但應該很容易解決...陣列deepcopy而不是perl對象中的引用
這裏的例子。
這裏的對象類
package obj;
use strict;
use warnings;
require Exporter;
our @ISA = qw(Exporter);
sub new(\@){
my $class=shift;
my $this={};
$this->{"array"}=shift;
return bless($this,$class);
}
sub getArray(){
my $this=shift;
return $this->{"array"};
}
和測試類
use strict;
use warnings;
use obj;
my @a=(1,2);
push @a,3;
my $ob=obj->new(\@a);
@a=();
print @{$ob->getArray()};
這個沒有返回值 - 不數組解引用轉變?
那麼該怎麼做?
thx
或通過做淺拷貝:'$這 - > { '陣列'} = [@ {移}]' – ysth
如果我這樣做,輸出將變成標量和和它只返回第一個數組元素1.我認爲深層複製是必要的,因爲我覆蓋@ a =()。所以被引用的數組不見了。使用Stable qw(dclone);做我想做的事。 thx – pyr0
@ pyr0,我不知道你是指我的代碼還是ysth,但既不復制數組的單個元素。 – ikegami