in reply to Difference of localtime

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).

Replies are listed 'Best First'.
Re^2: Difference of localtime
by ysth (Canon) on Feb 24, 2009 at 05:06 UTC
    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.

Re^2: Difference of localtime
by ikegami (Patriarch) on Feb 24, 2009 at 06:59 UTC

    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.