Taulmarill has asked for the wisdom of the Perl Monks concerning the following question:

oh wise monks, hear the question of thy humle brother,

iīve problems in understanding some things perl does with arrays.
my problem is, that print ((localtime)[4]); work well.
my $month = (localtime)[4]; works well too, but print (localtime)[4]; doesnīt work, but i donīt understan why.
could anybody give me a hint so i am able to understand this better?

Replies are listed 'Best First'.
Re: (localtime)[3] vs. ((localtime)[3])
by BrowserUk (Patriarch) on Sep 23, 2003 at 08:55 UTC

    Try

    print +(localtime)[4]; #or print( (localtime)[4] );

    To discover why this works, enable warnings:) and then read this extract from perlfunc:print

    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.

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
    If I understand your problem, I can solve it! Of course, the same can be said for you.

      Actually, you don't have to enable warnings, as the construct won't compile:
      $ perl -wle 'print(localtime)[3]' syntax error at -e line 1, near ")[" Execution of -e aborted due to compilation errors. $

      The warning you are referring to is silly. It'll trigger in constructs like:

      print (3) + 4

      but only if you have a single space between 'print' and the opening parenthesis.

      And you thought Python was evil because it gave meaning to whitespace.

      Abigail