in reply to Re: array element return syntax
in thread array element return syntax

H:\scripts>perl -e "print (localtime(time))[2]" syntax error at -e line 1, near ")[" Execution of -e aborted due to compilation errors.
8(

Replies are listed 'Best First'.
Re^3: array element return syntax
by davorg (Chancellor) on Mar 29, 2006 at 15:50 UTC

    -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

Re^3: array element return syntax
by ikegami (Patriarch) on Mar 29, 2006 at 15:46 UTC

    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];