Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??


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 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 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)).


When your only tool is a hammer, all problems look like your thumb.

In reply to Re: localtime parse within hash init by roboticus
in thread localtime parse within hash init by NetWallah

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • 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 or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2022-10-07 14:43 GMT
Find Nodes?
    Voting Booth?
    My preferred way to holiday/vacation is:

    Results (30 votes). Check out past polls.