I have a module with the following subroutines:
package module;
sub new
{
my $class = shift;
my $reference = shift;
bless $reference, $class;
return $reference;
};
sub add_row{
@newrow = [1,1,1];
push @_, @newrow;
};
@matricies is an array of array references. I created objects out of the array references using
my @object= map{module->new($_)} @matrices;
and lets say I want to add a row to one of the objects using:
@object[0]->add_row();
I think there’s something wrong with the add_row subroutine dealing with the use of @ and $. Any ideas?
Yes, you are adding an array ref yet your variable is an array.
The first thing you need to do (after using strict and warnings) is to read the following documentation on sigils in Perl (aside from good Perl book):
https://stackoverflow.com/a/2732643/119280 – brian d. foy’s excellent summary
The rest of the answers to the same question
This SO answer
The best summary I can give you on the syntax of accessing data structures in Perl is (quoting from my older comment)
Now, for your code:
should be
You have another problem in how to access your rows from the object:
You also have the same problem with
@object[0]->add_row();as with newrow – you are using array sigil to address 1 elementUPDATE: here’s a complete code:
Module’s add_row() (your constructor is fine):
Test driver:
Results: