http://qs321.pair.com?node_id=1106014


in reply to localtime parse within hash init

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:

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

Replies are listed 'Best First'.
Re^2: localtime parse within hash init
by NetWallah (Canon) on Nov 04, 2014 at 15:53 UTC
    thanks roboticus (++).

    That leads to:

    perl -E 'my %stuff=(otherinfo=>"this and that", dateinfo=> sub{ my %r; @r{qw|sec min hour mday mon year wday yday isdst|} =localtime(time); return \%r}->()); say "sec=",$stuff{dateinfo}{sec}, " year=",$stuff{dateinfo}{year}'
    Which works, is pretty decent, and free of external modules.

            "You're only given one little spark of madness. You mustn't lose it."         - Robin Williams