For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > September 2005 > oo newbie question









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author oo newbie question
Paulus

2005-09-29, 3:56 am

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

Tassilo v. Parseval

2005-09-29, 3:56 am

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
--
use bigint;
$n=7142335034377028016139702633033737113
9054411854220053437565440;
$m=-8,;;$_=$n&(0xff)<<$m,,$_>>=$m,,print+chr,,while(($m+=8)<=200);
Paulus

2005-09-29, 7:56 am

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

Paul

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com