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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.