PERL OOP newbie question: What is the best way to handle array data?

L

lovellj

Hi,

The only way I could get arrays to work with OOP in perl was to use a
reference to an array. I am wondering is this safe - ie is there any
chance that some part of the array may get overwritten in memory since
I am just using the reference in the functions? What is the best way
to do this?

I tried assigning $self->{'data_array'} = ("MON","TUE","WED"), but
then I found when I try to access this data, the array gets messed up
and it comes out as a scalar or as the last value in the array.


I apologize in advance if there is an obvious way to do this. Thanks
so much,
Jon


#####Test_class.pm
#!/usr/bin/perl -w
package Test_class;
use strict;
sub new
{
my $class = shift;
my $self = {};
my @arr = ("MON","TUE","WED");
$self->{'data_array'} = \@arr;
bless ($self,$class);
return $self;
}

sub function1
{
my $self = shift;
my @data_array = @{ $self->{'data_array'} };
###use the data...
###..... Is this safe - is there any chance parts of the array will
be deleted in memory
}
1;

###############
 
M

Martien verbruggen

On 30 Apr 2007 23:22:51 -0700,
Hi,

The only way I could get arrays to work with OOP in perl was to use a
reference to an array. I am wondering is this safe - ie is there any
chance that some part of the array may get overwritten in memory since
I am just using the reference in the functions? What is the best way
to do this?

I'm wondering what exactly you mean by 'get arrays to work'. Arrays
don't work. They're used to store data.
I tried assigning $self->{'data_array'} = ("MON","TUE","WED"), but
then I found when I try to access this data, the array gets messed up
and it comes out as a scalar or as the last value in the array.

Ah.

There is indeed some confusion in your head about what arrays are.

$self->{'data_array'} is a scalar. It is a single element out of a hash,
which is accessed via a reference to it, stored in $self. It cannot be
an array, because all you can store in a scalar, is a scalar. When you
assign a list to a scalar, you end up with the last element in the
scalar.

A lot of this is explained int he perlref documentation. It addresses
building complex data structures (which Perl objects most often are)
with references.

As an example:

If you want to store an array in a complex data structure, you do indeed
do that by storing a reference to the array. You can do that in several
ways:

# Store a reference to an anonymous array
my $foo = [1, 2, 3];

# Set $foo to the value 3, ignoring the other elements in the list
$foo = (1, 2, 3);

# access the first element
my $bar = $foo->[0];

# Store a reference to a named array
my @ar = (1, 2, 3);
$foo = \@ar;


I have the vague feeling that you're looking for the anonymous array
syntax.

Martien
 
P

Paul Lalli

The only way I could get arrays to work with OOP in perl was to use a
reference to an array. I am wondering is this safe - ie is there any
chance that some part of the array may get overwritten in memory since
I am just using the reference in the functions? What is the best way
to do this?

Martien's answer is quite correct and I strongly suggest you take it
in. I'd just like to point out that this really has nothing to do
with OOP directly. This is how all multi-dimensional structures work
in Perl. Objects in Perl simply happen to be (usually) defined as
hashes, and so any array attributes you want to ascribe them create a
multi-level structure.

You cannot store an array in an array, nor an array in a hash, nor a
hash in an array, nor a hash in a hash. Arrays and Hashes store lists
of scalars. Period. No way around that. References, however, are
scalars. Whether it's a reference to a scalar, reference to an array,
or reference to a hash, the reference itself is a scalar. Therefore,
you can store a *reference* to an array in either an array or a hash.
This is how mulit-level data structures are created in perl.

No, there is no danger of data getting over written. This is not C.
You are not using "pointers" that can be manipulated to accidentally
access other data. You are using references. Very different concept.

I strongly suggest you have a read of:
perldoc perlreftut
perldoc perllol
perldoc perldsc
perldoc perlref
(in that order)

Paul Lalli
 
J

Joe Smith

I tried assigning $self->{'data_array'} = ("MON","TUE","WED"), but
then I found when I try to access this data, the array gets messed up
and it comes out as a scalar or as the last value in the array.

That is correct; the comma operator in scalar context does exactly that.

You'll need to know when it is appropriate to use [] instead of ().

Using an array:
@array = ("MON","TUE","WED");
@array = qw(MON TUE WED);

Using a scalar (a reference to an anonymous array):
$array_ref = ["MON","TUE","WED"];
$array_ref = [qw(MON TUE WED)];
$self->{'data_array'} = [ qw(SUN MON TUE WED THU FRI SAT) ];

-Joe
 

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,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top