You are correct about what the @{ [ ] } syntax does in your first question. The reason it's written like that is twofold: first, you're trying to call a function within a string interpolation, which simple interpolation of scalar variables and the like doesn't allow. However, you can interpolate an array constructed on the fly just like you have. Second, the code (localtime)[5] means: evaluate the function localtime in list context, and then take the sixth element of the list that it returns. This also explains what is going on in your second example. This is known as a list slice and is explained in more detail in the "Slices" section of perldata.

Update: Thought I'd add some examples. As you've mentioned,

my $x = (localtime)[0];
works by taking a list slice, thus forcing localtime to be called in list context. The behavior of a list slice in scalar context, however, is to return the last element of the list, so
my $x = (localtime)[2,3,4,0];
would have the same result as the above. Another way to do it is
my ($x) = localtime;
This is different. There's no slice here. Instead, the parentheses around the left-hand side of the assignment force the right side to be evaluated in list context, and then the first element returned is assigned to the first scalar lvalue on the left side. BUT, parentheses do not always mean list context. In particular,
my $x = (localtime)
will not do the same thing. Instead it will call localtime in scalar context and assign the result to $x.


In reply to Re: Questions about context by Errto
in thread Questions about context 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.