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.


In reply to Accessing Arrays and Lists by Anonymous Monk

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.