in reply to array element return syntax

Close. You need parens around the list expression being indexed.

$hour = (localtime(time))[ 2 ];
You can also extract multiple elements:
my ($y, $m, $d) = (localtime(time))[ 5, 4, 3 ];

This feature is called a "list slice", and it's documented in perldata.

Replies are listed 'Best First'.
Re^2: array element return syntax
by Lyndley (Novice) on Mar 29, 2006 at 15:42 UTC
    H:\scripts>perl -e "print (localtime(time))[2]" syntax error at -e line 1, near ")[" Execution of -e aborted due to compilation errors.
    8(

      -w will help you understand what's going on.

      $ perl -we "print (localtime(time))[2]" print (...) interpreted as function at -e line 1. syntax error at -e line 1, near ")[" Execution of -e aborted due to compilation errors.

      use diagnostics is even better.

      $ perl -Mdiagnostics -we "print (localtime(time))[2]" print (...) interpreted as function at -e line 1 (#1) (W syntax) You've run afoul of the rule that says that any list op +erator followed by parentheses turns into a function, with all the list operators arguments found inside the parentheses. See perlop/Terms and List Operators (Leftward).

      So stop the parentheses looking like a function.

      $ perl -e "print +(localtime(time))[2]" 16
      --
      <http://dave.org.uk>

      "The first rule of Perl club is you do not talk about Perl club."
      -- Chip Salzenberg

      print is claiming your parens. In other words, Perl sees your code as
      print( ... )[2];
      To fix the problem, use
      print( (localtime(time))[2] );
      or
      print +(localtime(time))[2];