i tried several hours to store a sub array into an object and failed. maybe someone of you can show me how to store a deep copy with perl.
sry i dont know if this question is clear, but should be easy to solve…
here the example.
here the object class
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"};
}
and the test class
use strict;
use warnings;
use obj;
my @a=(1,2);
push @a,3;
my $ob=obj->new(\@a);
@a=();
print @{$ob->getArray()};
this returns nothing – does not shift dereference the array?
so how to do this?
thx
Deference what array? The only array involved in the shift is
@_?$_[0]is a scalar, not an array.A (shallow) array copy is done using:
so you want
If you truly want an deep copy (though there’s no need for it in your example), use