Just a couple of things:
- Use
Local::Module
instead of just module
. The Local
module space is reserved for non-CPAN modules. Also, by convention, Module names should start with an uppercase letter.
- Use
use strict
and use warnings
.
Let's look at your new
subroutine constructor:
sub new
{
my $class = shift;
my $reference = shift;
bless $reference, $class;
return $reference;
};
I'm not sure what you're attempting to do here. Normally, this should be a constructor. You get a reference object back, you don't pass it a reference. Maybe you mean this?
package Local::Module;
sub new {
my $class = shift;
my $reference = {};
bless $reference, $class;
return $reference;
}
This wil create a NEW object you can use for adding. So, you'd do this first:
my $object = Local::Module->new;
Now, you can use $object
as a handle to your rows:
sub add_rows {
my $self = shift;
my $rowRef = shift;
if (not exists $self->{ROWS}) {
$self->{ROWS} = [];
}
push @{$self->{ROWS}}, $rowRef;
}
Now, you can use this object to add rows:
my $object->add_row = $RowReference;
Notice that an object in Perl is usually a reference to an anonymous hash. You put the data you want in one of the keys of your hash. In this case, you put your array in $self->{ROWS}.
Yes, there are all sorts of ways to create pseudo hashes, and inside out hashes, but the idea is that your class is normally not the actual data, but a reference to an object that holds the data. Otherwise, you're not really using object oriented programming.
In your case, I wouldn't bother with map
. I doubt it'll be more efficient, and a for loop will be cleaner (untested):
use strict;
use warnings;
my @matrices = ([1,0,0],[0,1,0],[0,0,1]);
my @objects;
foreach my $array_ref (@matrices) {
my $module_ref = Local::Module->new;
my $module_ref->add_row($array_ref);
push @objects, $module_ref;
}
package Local::Module;
sub new {
my $class = shift;
my $reference = {};
bless $reference, $class;
return $reference;
}
sub add_rows {
my $self = shift;
my $rowRef = shift;
if (not ref($rowRef) eq "ARRAY") {
die qq(Method "add_rows" can only take an Array reference);
}
if (not exists $self->{ROWS}) {
$self->{ROWS} = [];
}
push @{$self->{ROWS}}, $rowRef;
}
Now, your list of @objects
is a list of Local::Module
classes. You could now do something like this:
$objects[2]->add_row($row_ref);
This is a very rough outline. For example, you probably want another module that will return a reference to all of your arrays. Maybe another that can pop and shift the rows in your array, and return the popped or shifted row.
You also might want to include a way to pass in the initial reference to an array:
sub new {
my $class = shift;
my $array_ref = shift;
my $reference = {};
bless $reference, $class;
if (defined $array_ref) {
$reference->add_row($array_ref);
}
return $reference;
}
Note that once I bless $reference
, I can use it in object oriented calls. This way, my constructor has no idea what my object looks like. There's a general idea that constructor and methods should be ignorant of how the rest of the object is structured. That way, when you modify the object, you only have to modify one or two isolated methods. If I change the way my method add_rows
work, I don't have to modify my constructor. The change is isolated in a single location.