oo newbie question

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::Dumper;
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
 
T

Tassilo v. Parseval

Also sprach Paulus:
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::Dumper;
my $bar = foo->new;
my @source = [1,2,3];

Here you store a reference to an anonymous array (containing the
elements 1, 2, 3) in the first element of @source. What you more likely
want is

my @source = (1, 2, 3);
$bar->arr(@source);

Here you pass the array containing the array-reference to arr().
my @apple = $bar->arr;
print $bar->arr."\n";

This produces 1 because you are using the '.' operator which evaluates
its operands in scalar context. Note that evaluating an array in scalar
context yields its length. Since you stored a reference to an array as
the first element of that array, the length of that array is 1.
print @apple."\n";

The same problem here: You're evaluating @apple in scalar context.

If you had written

print @apple, "\n"; # list context

you'd still not get the desired output. The output would be something
like

ARRAY(0x814cbb8)

because @apple has only one element: the already mentioned
array-reference.
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?

What you neglect is that Data::Dumper() expects a _reference_. If you
really want to see what the return value of $bar->arr looks like,
rewrite that line to

print Dumper \$bar->arr;

and look at the output again. I'm sure you'll spot the difference.
How do I access the array
elements? I expected @apple to be a copy of _arr ...

It is a copy of @{ $self->{_arr} }. Your problem was that _arr didn't
contain what you thought it did.

I am not sure if your problems are really with OO. What you may actually
need is a refresher on Perl's data-structures and references. Just skim
once again through 'perldoc perldata', "List value constructors". Also
read about "Context" in the same document. And in case you don't feel
entirely comfortable, 'perldoc perlreftut' will also help you.

Tassilo
 
P

Paulus

Thank you very much for your explanation! I'll work on my knowledge of
data structures and references ;)

Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top