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;
###############
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;
###############