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

Please tell why parenthesis make a differense here:
# perl -e 'use Time::Piece; $x = localtime; $y = localtime() - $x; pr +int "$y\n"; ' 0 # perl -e 'use Time::Piece; $x = localtime; $y = (localtime) - $x; pr +int "$y\n"; ' 0 # perl -e 'use Time::Piece; $x = localtime; $y = localtime - $x; print + "$y\n"; ' Thu Jan 1 01:00:00 1970

Replies are listed 'Best First'.
Re: Time::Piece, localtime -- ambiguous, parentheses
by Eily (Monsignor) on Dec 05, 2018 at 16:57 UTC

    This is one of the cases where MO::Deparse can help, by showing how perl interpretes your code:

    perl -MO=Deparse -le 'use Time::Piece; $x = localtime; $y = localtime +- $x; print $y;' BEGIN { $/ = "\n"; $\ = "\n"; } use Time::Piece; $x = main::localtime(); $y = main::localtime(-$x); print $y; -e syntax OK
    localtime - $x is actually interpreted as localtime(-$x)

Re: Time::Piece, localtime -- ambiguous, parentheses
by haukex (Archbishop) on Dec 05, 2018 at 17:04 UTC

    Eily beat me to it, but since I've already typed it up... ;-) B::Deparse to the rescue - the first two are localtime() - $x, while the third is localtime(-$x). The same is true if you're not using Time::Piece, which overrides localtime, but you also get a helpful warning. Interestingly, core localtime has a prototype, while Time::Piece::locatime doesn't.

    $ perl -MTime::Piece -le 'print prototype("CORE::localtime"); print pr +ototype("Time::Piece::localtime")//"undef"' ;$ undef $ perl -MO=Deparse -e 'my $x = localtime; my $y = localtime - $x; prin +t "$y\n"; ' Warning: Use of "localtime" without parentheses is ambiguous at -e lin +e 1. my $x = localtime; my $y = localtime -$x; print "$y\n"; -e syntax OK $ perl -MO=Deparse -e 'use Time::Piece; my $x = localtime; my $y = loc +altime() - $x; print "$y\n"; ' use Time::Piece; my $x = main::localtime(); my $y = main::localtime() - $x; print "$y\n"; $ perl -MO=Deparse -e 'use Time::Piece; my $x = localtime; my $y = (lo +caltime) - $x; print "$y\n"; ' use Time::Piece; my $x = main::localtime(); my $y = main::localtime() - $x; print "$y\n"; $ perl -MO=Deparse -e 'use Time::Piece; my $x = localtime; my $y = loc +altime - $x; print "$y\n"; ' use Time::Piece; my $x = main::localtime(); my $y = main::localtime(-$x); print "$y\n";

      -MO=Deparse,-p is even clearer

Re: Time::Piece, localtime -- ambiguous, parentheses
by 1nickt (Canon) on Dec 05, 2018 at 17:09 UTC

    Hi, ... and even without deparsing, you can see that under Time::Piece what localtime returns is different to what it assigns to a variable in scalar context:

    $ perl -MTime::Piece -E 'say localtime; $x = localtime; say $x' 4351751111833380 Wed Dec 5 17:05:43 2018

    Hope this helps!


    The way forward always starts with a minimal test.
Re: Time::Piece, localtime -- ambiguous, parentheses
by leszekdubiel (Scribe) on Dec 05, 2018 at 17:38 UTC
    Thank you for making such a great and helpful Perl community :)