I'm working on creating a Graph class in Perl and I'm hoping to have each Graph contain Nodes. Each Node has some properties and also (for now) an array which contains references to each other node it is connected to. So far I have this definition of a node:
use strict; package Node;
sub new{
my $class = shift;
my @array = ();
my $array_r = \@array;
my $self = {
code => undef,
name => undef,
country => undef,
continent => undef,
timezone => undef,
coordinates => undef
population => undef,
region => undef,
arrayRef => $array_r,
@_,
}; bless $self, $class; return $self; } Yet upon calling the following function from my main script:
sub getSetArray{
my $self = shift;
my $param = shift;
my $temp = $self->{arrayRef};
push(@{$temp}, $param) if defined $param;
return $self->{arrayRef};
}
and trying to iterate over a Node's array(which will contain more Nodes that it is connected to):
my $firstnode = Node->new(); # Node constructor should have @array of
my $secondnode = Node->new();
$firstnode->getSetCode("test");
print "The current code is ", $firstnode->getSetCode(), "\n";
my $array_r = $firstnode->getSetArray($secondnode);
$array_r = $firstnode->getSetArray($firstnode);
foreach my $obj (@{$array_r}){
print $obj;
}
This prints out Node=HASH(0x10092bb00)Node=HASH(0x100907bd8). Which leads me to believe that I am dealing with an array containing 2 nodes (this is what I want). But upon attempting to call any methods of this $obj's I am told that I
Can't call method "getSetNode" without a package or object reference.
I had already previously blessed these two objects when calling new on these nodes. So I'm not sure why they aren't recognized as Nodes and I can't call their methods....
EDIT -
foreach my $obj (@{$array_r}){
print $obj->getSetCode();
}
where getSetCode() is
sub getSetCode{
my $self = shift;
my $param = shift;
$self->{code} = $param if defined $param;
return $self->{code};
}