NetWallah:
It may not be particularly elegent, but I'd just create a sub that returns the hash.
sub date_hash {
my ($dtm, $rv) = (shift // time, {});
@{$r}{qw(sec min hour mday mon year wday yday isdst)} = localtime(
+$dtm);
return $rv;
}
My reasons are:
- This gives you the opportunity to give it a name that makes your initializer easy to read:
my %stuff=(otherinfo=>"this and that", dateinfo=>{%{date_hash($aTime)}});
- I find that when I need something like this in a program, it may start out being needed only once, but frequently gets used a few more times.
- It moves an ugly bit of code out of the way, usually into a projFooUtils.pm file, which often has a handful of routines simple enough to never have to look at again.
Update: On the way in to work, I remembered that I *have* a similar function in my MCMUtils.pm package already, with a couple of changes:
- I can never remember the exact abbreviations to use for the date/time chunks, so I use my 'standard' values: YYYY, YY, MM, DD, hh, mm, ss, dow
- I go ahead and "fix" the month (+1) and year values (YYYY={year}+1970, YY=({year}+1970)%100).
- I let the user send in the keys desired. So if they send in YYYY, MM, DD that's all I'll return. But if no keys are sent in, it returns all of 'em. Yeah, it makes it a little overcomplicated, perhaps, but I use it in a good few places.
I'll try to remember to update this node with the code for that function when I get back home. (It's not in my $work util packages (yet)).
...roboticus
When your only tool is a hammer, all problems look like your thumb.
-
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.
|