Wednesday, August 13, 2014

It's 1975 all over again

This one had me confused for a moment:
$ date
Wed Aug 13 14:34:53 EDT 2014
$ re.pl
$ # The join() is only for cosmetic reasons
$ join " " => localtime
6 36 14 13 7 114 3 224 1
$ # Let's grab only the HH:MM part (items 2 and 1)
$ my ($h, $m) = localtime[2,1]
$VAR1 = 56;
$VAR2 = 44;
Huh?

Oh, I see: I forgot my parentheses.
$ ($h, $m) = (localtime)[2,1]
$VAR1 = 14;
$VAR2 = 41;
Much better.  But where the hell did the first set of values come from?
$ join " " => localtime[2,1]
56 31 8 17 10 75 1 320 0
$  scalar localtime[2,1]
Mon Mar 10 02:35:04 1975
Dang, I forgot that localtime() can take an argument, a Unix time number (just like localtime(3) takes a time_t as argument).  This is all starting to make sense.
$ [2,1] . ''
ARRAY(0x9f9dd0c)
$ [2,1] + 0
164751992
$ scalar localtime 164751992
Sat Mar 22 15:26:32 1975
(I'm still a bit surprised that Perl didn't say a word about this.)

No comments: