Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > oo newbie question

Reply
Thread Tools

oo newbie question

 
 
Paulus
Guest
Posts: n/a
 
      09-29-2005
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

 
Reply With Quote
 
 
 
 
Tassilo v. Parseval
Guest
Posts: n/a
 
      09-29-2005
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:umper;
> 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(0x814cbb

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:umper() 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=71423350343770280161397026330337371139054411854 220053437565440;
$m=-8,;;$_=$n&(0xff)<<$m,,$_>>=$m,,print+chr,,while(($ m+=<=200);
 
Reply With Quote
 
 
 
 
Paulus
Guest
Posts: n/a
 
      09-29-2005
Thank you very much for your explanation! I'll work on my knowledge of
data structures and references

Paul

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
VONAGE Newbie w/newbie question New_kid@nowhere.new VOIP 0 08-11-2007 01:40 PM
another newbie question from another newbie.... Lee UK VOIP 4 05-17-2005 04:10 PM
newbie: cisco vlan newbie question No Spam Cisco 3 06-07-2004 10:02 AM
dumb newbie question (or newbie dumb question) Jerry C. Perl Misc 8 11-23-2003 04:11 AM
Newbie! I'm a newbie! What's wrong with this program? Id0x Python 4 07-20-2003 11:40 PM



Advertisments