The only way this (my) code can throw a division by zero error is when you tell the program to read an empty data file. This can be mitigated.
sub average_hourly_wage{
my $self = shift;
my $wages = 0; my $count = 0;
foreach my $employee ( @{ $self->{EmployeeList} })
{
$wages += $employee->{hourlywage}; $count++;
}
return $count ? $wages / $count : 0;
}
And of course, if the data file is empty, there is nothing in the Employee-List, thus
->find returns undef. That however, is something you have to deal with in the calling code.
if ( my $e = $EmployeeList->find("holli") )
{
print $e->pay, "\n";
}
else
{
print "User not found\n";
}
holli
You can lead your users to water, but alas, you cannot drown them.