P
Paulus
Hi!
I'm new to oo programming in perl. I'm trying to access an array in a
class but can't understand what I'm getting.
This is my class, It contains an array and a method to access that
array:
package foo;
sub new {
my $self = {
_arr => []
};
return bless $self;
}
sub arr {
my $self = shift;
if (@_) { @{$self->{_arr}} = @_ }
return @{$self->{_arr}};
}
1;
This is my test program:
use strict;
use foo;
use Data:umper;
my $bar = foo->new;
my @source = [1,2,3];
$bar->arr(@source);
my @apple = $bar->arr;
print $bar->arr."\n";
print @apple."\n";
print Dumper($bar->arr);
It gives the following output:
1
1
$VAR1 = [
1,
2,
3
];
I fail to understand why the first print gives '1' ... as I can see
with Dumper, the array is stored properly? How do I access the array
elements? I expected @apple to be a copy of _arr ...
Thanks for your help!
Paul
I'm new to oo programming in perl. I'm trying to access an array in a
class but can't understand what I'm getting.
This is my class, It contains an array and a method to access that
array:
package foo;
sub new {
my $self = {
_arr => []
};
return bless $self;
}
sub arr {
my $self = shift;
if (@_) { @{$self->{_arr}} = @_ }
return @{$self->{_arr}};
}
1;
This is my test program:
use strict;
use foo;
use Data:umper;
my $bar = foo->new;
my @source = [1,2,3];
$bar->arr(@source);
my @apple = $bar->arr;
print $bar->arr."\n";
print @apple."\n";
print Dumper($bar->arr);
It gives the following output:
1
1
$VAR1 = [
1,
2,
3
];
I fail to understand why the first print gives '1' ... as I can see
with Dumper, the array is stored properly? How do I access the array
elements? I expected @apple to be a copy of _arr ...
Thanks for your help!
Paul