I can’t figure out what’s going on here. Where does the 8 below come from?
Time::HiRes provides an overload of stat which expands the times to have high-resolution (which is supported on my system).
$ perl -MTime::HiRes -e 'print +(stat("foo"))[8], "\n"' # V1
1322915623
$ perl -MTime::HiRes=stat -e 'print +(stat("foo"))[8], "\n"' # V2
8
$ perl -MTime::HiRes=stat -e '@a = stat("foo"); print $a[8], "\n"' # V3
1322915623
That particular file doesn’t have a high-resolution timestamp, but that’s not the mystery: the mystery is V2, which prints 8. In fact, it always prints the number in square brackets.
The obvious answer, it parses differently, does not seem correct:
$ perl -MO=Deparse -MTime::HiRes -e 'print +(stat("foo"))[8], "\n"' # V1
use Time::HiRes;
print((stat 'foo')[8], "\n");
-e syntax OK
$ perl -MO=Deparse -MTime::HiRes=stat -e 'print +(stat("foo"))[8], "\n"' # V2
use Time::HiRes (split(/,/, 'stat', 0));
print((stat 'foo')[8], "\n");
-e syntax OK
They deparse the same (other than the different option to use Time::HiRes).
It works fine if I use my own function in similar syntax, and I can’t get the “wrong” answer even if I return something silly from my function:
$ perl -e 'sub bar() { return qw(a b c d e f g h i j) }; print +(bar)[8], "\n"'
i
$ perl -e 'sub bar() { return undef }; print +(bar)[8], "\n"'
$
This is Debian’s perl package, version 5.14.2-5. I get the same results with 5.10.1-17squeeze2.
How does V2, above, produce 8? Am I misunderstanding Perl syntax in some way, or do I just need to file a bug report?
edit: As @cjm says, this is a bug. It has been fixed in Time-HiRes-1.9725 according to the report.
It’s definitely a bug, although I’m not sure whether it’s in core Perl or in Time::HiRes. I get the same results with Perl 5.14.2 on Gentoo (and also with 5.8.9 and 5.10.0). Have you noticed that it doesn’t matter what you put in the subscript?
I’d probably report it in Time::HiRes first.
Note: While they deparse the same, they do generate different opcodes (due to the difference between calling a built-in and a user-defined sub):