Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Difference of localtime

by vinoth.ree (Monsignor)
on Feb 24, 2009 at 03:27 UTC ( [id://745882]=perlquestion: print w/replies, xml ) Need Help??

vinoth.ree has asked for the wisdom of the Perl Monks concerning the following question:

my $now = localtime time; print "It is now $now"; my $re= scalar localtime; print "It is now $re"; In the above code any different is there or both are same.

Replies are listed 'Best First'.
Re: Difference of localtime
by almut (Canon) on Feb 24, 2009 at 04:29 UTC

    Depends on what you mean by "code any different?".

    In case you want to know if the internally generated opcodes are the same, you can use perl -MO=Concise ... to show them (see B::Concise).  In this case you'd see that the code is different, i.e. scalar is still there (not optimised away, Update: see ysth's note) in the one case, despite being redundant, and time is being called explicitly in the other case, despite being the default with localtime anyway:

    ... 2 <;> nextstate(main 1 745882.pl:4) v ->3 6 <2> sassign vKS/2 ->7 4 <1> localtime[t3] sK/1 ->5 3 <0> time[t2] s ->4 5 <0> padsv[$now:1,2] sRM*/LVINTRO ->6 7 <;> nextstate(main 2 745882.pl:5) v ->8 ... vs. ... 2 <;> nextstate(main 1 745882.pl:7) v ->3 5 <2> sassign vKS/2 ->6 - <1> scalar sK/1 ->4 3 <0> localtime[t2] s ->4 4 <0> padsv[$re:1,2] sRM*/LVINTRO ->5 6 <;> nextstate(main 2 745882.pl:8) v ->7 ...

    That said, the code will produce the same output... (if you happen to execute both statements immediately one after another, and unless you happen to pass a seconds boundary in between the two calls).

      Perl's opcodes are arranged in an execution tree as well as the parse tree. The - to the left indicates that scalar was optimized away and is not in the execution tree. -MO=Concise,-exec shows only the execution tree.

      The s to the right of localtime indicates scalar context in both cases.

      Furthermore, scalar is optimized away even when it isn't redundant, since scalar only has a compile-time effect.

      >perl -MO=Concise -e"@a = scalar @b" a <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v:{ ->3 9 <2> aassign[t5] vKS ->a - <1> ex-list lK ->6 3 <0> pushmark s ->4 - <1> scalar sK/1 ->- <--- Note leading dash 5 <1> rv2av[t4] sK/1 ->6 <--- "s" means "scalar" 4 <#> gv[*b] s ->5 did its job. - <1> ex-list lK ->9 6 <0> pushmark s ->7 8 <1> rv2av[t2] lKRM*/1 ->9 7 <#> gv[*a] s ->8 -e syntax OK

      It's impossible for scalar to work at run-time, since operands are evaluated before their operator.

Re: Difference of localtime
by ww (Archbishop) on Feb 24, 2009 at 03:55 UTC
    perl -e "my $now = localtime time; print \"It is now $now \"; my $re= scalar localtime; print \"It is now $re \";" on Windows...

    or, for a nix-ish OS...

    perl -e 'my $now = localtime time; print "It is now $now"; my $re= scalar localtime; print "It is now $re";'

    Go ahead. Try it. In fact, you can often answer your own questions, by such trials.

    Or did you mean to ask a question that's not evident, such as "Does using scalar make a difference in the output? (Answer: not here) or "Does changing the variable's name make a difference?" (Answer: $now is certainly easier to understand than $re) or "Will one be quicker than the other?" (Answer: Benchmark it.)

    And please, you've been here long enough to know that code -- even snippets like these -- should be wrapped in <c>...</c> tags.

    Update: Fixed missing </p> tag; clarified phrasing of first question.

      Or for both
      perl -e warn-localtime(time);warn-localtime -Tue Feb 24 00:35:27 2009 at -e line 1. -Tue Feb 24 00:35:27 2009 at -e line 1.
Re: Difference of localtime
by Lawliet (Curate) on Feb 24, 2009 at 03:47 UTC
    $ perl -E 'say localtime time; say scalar localtime' 1946222311091530 Mon Feb 23 22:46:19 2009
    $ perl -E'$foo = localtime time; say $foo; $bar = localtime; say $bar' Tue Feb 24 16:34:31 2009 Tue Feb 24 16:34:31 2009

    You be the judge! :) (This time, be the judge with misleading code. Thanks, ysth and Annony.)

    I guess I should have suggested for you to just try it yourself to begin with~

    Update: Added smiley

    And you didn't even know bears could type.

      That is misleading because  my $foo = localtime; is scalar context.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://745882]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-04-24 05:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found