in reply to array element return syntax

This also works. Update: Not the ideal solution, but at the very least a TIMTOWTDI example.
print [localtime(time)]->[5];

You can also use parentheses, as posted previously. However, when dealing with print, for example, you need to let the compiler know that your parens are for wrapping the function and not part of the print statement. This will work for that:
print +(localtime(time))[5];

---
It's all fine and dandy until someone has to look at the code.

Replies are listed 'Best First'.
Re^2: array element return syntax
by merlyn (Sage) on Mar 29, 2006 at 15:56 UTC
    This also works.
    For some meaning of "works", in the same sense that you can add "sleep 1" in various places in your program and it still "works", albeit slower.

    There's no point in constructing an entire full-fledged array, then take a reference to it, then dereference that reference to get a particular element, then garbage collect the full array, just to get one element of it. It's a lot of extra work.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      Thanks! That makes sense. It was just the first thing that sprang to my mind, but now I know better.

      ---
      It's all fine and dandy until someone has to look at the code.
Re^2: array element return syntax
by ikegami (Patriarch) on Mar 29, 2006 at 15:44 UTC
    And the slice "equivalent" would be
    my ($y, $m, $d) = @{[localtime(time)]}[ 5, 4, 3 ];

    These construct an array, which makes them slower.

Re^2: array element return syntax
by Lyndley (Novice) on Mar 29, 2006 at 15:53 UTC
    H:\scripts\7d>perl -e "print ((localtime(time))[2])" 16
    =D Excellent thankyou. Could I be cheeky and ask just how print ((localtime(time))[2]) works at each step as regards each context

    From :

    time

    To :

    print ((localtime(time))[2])?

      1. time returns a number that represents the number of seconds since the start of the epoch.
      2. localtime(time) returns a list of values that represent the seconds, minutes, etc. in the local time zone
      3. (localtime(time))[2] indexes into the list (a convenience that perl allows) and returns the 3rd item in the list
      4. print((localtime(time))[2]) outputs the 3rd item in the list returned by localtime

      The only "weird" part might be why there are extra parens on the print. I.e., why not

      print (localtime(time))[2];
      And the answer is precedence. To perl, that looks like a function call (to the print function) with a parameter of localtime(time). So it's really
      print(...)[2];
      Which doesn't make any sense, so perl tells you so.