in reply to Can you explain the difference with print?

That's a classic mistake: when Perl sees print followed by an opening paren, it interprets that as print(...), i.e. like any regular function call foo(...), and then it doesn't understand the [6] following the function call. You need to disambiguate using one of several methods - the most explicit IMHO is an extra set of parentheses, print( (localtime)[6] );. Or better yet, just use one of the nicer OO interfaces like Time::Piece.

Replies are listed 'Best First'.
Re^2: Can you explain the difference with print?
by hippo (Archbishop) on Aug 05, 2023 at 11:00 UTC
    Or better yet, just use one of the nicer OO interfaces like Time::Piece

    Agreed - the interface is so much easier to work with and for this case requires no brackets at all.

    perl -MTime::Piece -we 'print localtime->day_of_week'

    🦛

Re^2: Can you explain the difference with print?
by Bod (Parson) on Aug 05, 2023 at 23:27 UTC
    That's a classic mistake

    Thanks for the explanation - it's now very clear :)

    Or better yet, just use one of the nicer OO interfaces...

    I was doing that oneliner to remind myself what [6] was as I was working on a bit of pre-existing code. It has now been converted to use Time::Piece's localtime->wdayname so that the rest of the code uses day names instead of magic numbers.