Why can't I say:
print (gmtime(time))[2];
Well, if you had enabled warnings, you would have seen useful
information:
D:\> perl -we "print (gmtime(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.
OTOH:
D:\> perl -we "print +(gmtime(time))[2]"
22
Also, if you had checked the documentation for for print, you would have
read:
D:\> perldoc -f print
... Because print takes a
LIST, anything in the LIST is evaluated in list context, and any
subroutine that you call will have one or more of its
expressions evaluated in list context. Also be careful not to
follow the print keyword with a left parenthesis unless you want
the corresponding right parenthesis to terminate the arguments
to the print--interpose a "+" or put parentheses around all the
arguments.
....
@foo = gmtime(time);
print @foo[2];
What's wrong with the first syntax?
Actually, the @foo[2] syntax is wrong as well. You are selecting a slice
consisting of the third element of foo. You should use $foo[2] access
the third element of an array foo.
You should also read the posting guidelines for this group.
Sinan.