I am new to using OO Perl and am having some difficulties gettting started. Basically, I have defined a class called Connection that defines a Connection object. A Connection is a tcp connection, so an instance of the class stores a source IP, source port, destination IP, etc. I have tested the class and as far as I can tell, it is working correctly. However, I want to create an array of Connections to keep track of all the connections in an tcpdump trace file. Perl doesn't complain when I create a new Connection and push it onto an empty array called @connections. However, when I try to reference that object I get an error. Here is some of the relevant code:
my @connections = {};
my $new_connection = eval { Connection->new(); } or die (@_);
$new_connection->sourceIP($sIP);
$new_connection->destinationIP($dIP);
push(@connections, $new_connection);
# loop through connections array
my $index;
for($index=0; $index<$#connections; $index++) {
if ($connections[$index]->sourceIP() eq $sIP) {
# do some stuff
}
Like I said, there are no compilation errors, and there are no errors in creating the $new_connection object or pushing it onto the empty array. The run-time error I am getting comes from the line:
if ($connections[$index]->sourceIP() eq $sIP )
The error I am getting is :
Can't call method "sourceIP" on unblessed reference at line 69
However, the object is blessed. I have tested just declaring a single object and that seems to be working. It is only when I try to refer to it in an array that I get the problem.
I am not sure if I can even declare an array of a user-defined class. Is this possible, and if not how can I go about storing large amounts of instances of my class?
Thanks,
skm376