Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
I'm a fan of brevity and one-liners, so here goes... I've been trying to access one element from an array, and the man pages tell me this:
$time = (stat($file))[8]; print $time;
That works fine and dandy, but then I can't do something like:
print (stat($file))[8];
(I hate temporary variables...) Perl gives me an error (Can't use subscript on scalar at - line 1, near "8]") and I'm not quite too sure why. On the other hand, in also needing to find the length of a hash, the page for scalar tells me I can force list context by doing the following:
print ${[stat($file)]}[8];
Which works fine. So, my question is, what's the difference between the two and why doesn't the first one work? // After a little more reading, I could deduce that the second method is accessing a singular element of a resolved array reference, which is defined by a list.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Accessing Arrays and Lists
by demerphq (Chancellor) on Jun 27, 2002 at 18:35 UTC | |
by danger (Priest) on Jun 27, 2002 at 18:50 UTC | |
by demerphq (Chancellor) on Jun 27, 2002 at 19:45 UTC | |
|
Re: Accessing Arrays and Lists
by Aristotle (Chancellor) on Jun 27, 2002 at 21:56 UTC |