storing @AoA (or any data structures) in classes

I'm trying my hand out at some OOP perl.  It's a bit confusing.

Suppose I have a class MyClass.  There are functions such as readCSV and printCSV.  I've done some small class examples using scalar variables stored as "member" data.  But how is this done with arrays, hashes, and complex data structures?  In this example, I'd like to store the @AoA as member data in the class instance.


use strict;
 
package MyClass;
 
sub new
{
   my $class = shift;
   my %params = @_;
   my $self = {};
   bless($self,$class);
   return $self;
}
 
sub readCSV 
{
   my $self = shift;
   my $file = shift;
   open(FILE,"$file");
   while(<FILE>){
      chomp;  
      push push $self->@AoA, [split /,/]; #???
   }
   close(FILE);
}
 
sub printCSV
{
   my $self = shift;
}
 
1;

Open in new window

mock5cAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kawasCommented:
just assign the your member name to be the array (or array reference), i.e.
sub new
{
   my $class = shift;
   my %params = @_;
   my $self = {};
   #store your array
   $self->{array_of_array} = @AoA;
   bless($self,$class);
   return $self;
}

Open in new window

0
Adam314Commented:
This:
    $self->{array_of_array} = @AoA;
will interpret @AoA in scalar context, returning the number of elements in @AoA, and so setting $self->{array_of_array} to the number of elements in @AoA.

You will have to us a reference to @AoA.  You could do either of these:
    #Create a reference to @AoA.  Changes to @{$self->{array_of_array}} will change @AoA
    $self->{array_of_array} = \@AoA;  
   
    #Create a reference to a new array containing the same data as @AoA.
    #Changes to @{$self->{array_of_array}} will not change @AoA
    #    (but changes to the contents of rows will change the same rows in @AoA)
    $self->{array_of_array} = [@AoA];

See here for more on references:
    http://perldoc.perl.org/perlreftut.html
    http://perldoc.perl.org/perldsc.html
    http://perldoc.perl.org/perllol.html

sub readCSV 
{
    my $self = shift;
    my $file = shift;
    open(FILE,"<$file") or die "Could not open $file: $!\n";
    my @lines=<FILE>;
    chomp @lines;
    $self->{lines} = [map {[split/,/]} @lines];
    close(FILE);
}
 
sub PrintCSV
{
    my $self=shift;
    foreach (@{$self->{lines}}) {
        print join(",", @$_) . "\n";
    }
}
 
1;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.