Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^5: Newbie question under time constraint

by holli (Abbot)
on Aug 04, 2019 at 21:14 UTC ( [id://11103887]=note: print w/replies, xml ) Need Help??


in reply to Re^4: Newbie question under time constraint
in thread Newbie question under time constraint

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.

Replies are listed 'Best First'.
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11103887]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-04-24 06:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found