Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: When hashes aren't enough

by BigLug (Chaplain)
on May 26, 2004 at 12:10 UTC ( [id://356550]=note: print w/replies, xml ) Need Help??


in reply to When hashes aren't enough

Many wiser monks have had their say, so let me add my own $0.02.

If you're just storing data, use a Hash of Hashes (countles examples above).

If your data should be intelligent, use Objects. Consider and example similar to that in perltoot:

You run a business, you employ people (class 'Employee'). They all do different things, they all get paid different amounts and all work different hours. So, in procedural programming we'd have to know all this and branch around the place like crazy. With OO, it's all under the bonnet and we dont need to touch anything:

$roger = new Employee::Casual( rate => 14.00 ); $sally = new Employee::FullTime( salary => 50_000 ); $mark = new Employee::FullTime( wage => 12.00 ); $roger->add_hours( 8 ); # worked 8 hours $sally->add_hours( 8 ); # worked 8 hours $mark->add_hours( 8 ); # worked 8 hours # Now pay them $roger->write_check(); # 14 * 8 = $112.00 $sally->write_check(); # $0.00 - nothing due to a full timer until +fortnight is accounted for $mark->write_check(); # $0.00 - same here # add another 11 days # ... same as above, 11 times ... # then on day 13, it's a long day $roger->add_hours( 10 ); # worked 10 hours $sally->add_hours( 10 ); # worked 10 hours $mark->add_hours( 10 ); # worked 10 hours # So then they take day 14 off $roger->sick_leave( 8 ); # 8 hours $sally->sick_leave( 8 ); # 8 hours $mark->sick_leave( 8 ); # 8 hours # and get a work log: $roger->write_check(); # 11 days * 8 hours + 1 day * 10 hours = 98 hours @ $14.00 = $13 +72 # (no sick pay for casuals and we've already paid him for day 1) $sally->write_check(); # 14 days @ ($50000 / 365 days) = $1917 # (no overtime for sallary earners) $mark->write_check(); # 12 days * 8 hours + 1 day * 10 hours = 106 hours @ $12.00 = $1 +272 # get entitlements $roger->leave_entitlements(); # {sick_leave => 0, annual_leave => 0 +} - none for the casual $sally->leave_entitlements(); # {sick_leave => 56, annual_leave => + 8.6} $mark->leave_entitlements(); # {sick_leave => 56, annual_leave => +8.6}
As you can see, the objects are intelligent, so it doesn't matter that we tell all the objects ($roger, $sally and $mark) exactly the same thing, they all know, under the hood, what to do.

To learn how to do that, read perltoot. It's not only a good tutorial, it's an easy read and you'll come away having learned something about perl that you didn't know before. Plus, learning OO is very PerlMonk.

bless($me, $ISA[0]) for $i (sin($have));
"Get real! This is a discussion group, not a helpdesk. You post something, we discuss its implications. If the discussion happens to answer a question you've asked, that's incidental." -- nobull@mail.com in clpm

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (7)
As of 2024-04-24 10:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found