in reply to Re: OOP - Constant Vs. Subroutine ()
in thread OOP - Constant Vs. Subroutine

by adding "()" after each's name
...which is exactly the reason why perl can optimize the sub away at compile time and makes them behave like use constant- note the two invocations
qwurx [shmem] ~ > perl -MO=Concise,-exec -e 'use constant foo=>"5"; $c +=4+foo;' 1 <0> enter 2 <;> nextstate(main 70 -e:1) v 3 <$> const[IV 9] s 4 <#> gvsv[*c] s 5 <2> sassign vKS/2 6 <@> leave[1 ref] vKP/REFC -e syntax OK
qwurx [shmem] ~ > perl -MO=Concise,-exec -e 'sub foo(){"5"}; $c=4+foo; +' 1 <0> enter 2 <;> nextstate(main 2 -e:1) v 3 <$> const[IV 9] s 4 <#> gvsv[*c] s 5 <2> sassign vKS/2 6 <@> leave[1 ref] vKP/REFC -e syntax OK

are identical (except for the code of constant.pm loaded in the first), while

qwurx [shmem] ~ > perl -MO=Concise,-exec -e 'sub foo{"5"}; $c=4+foo;' 1 <0> enter 2 <;> nextstate(main 2 -e:1) v 3 <$> const[IV 4] s 4 <0> pushmark s 5 <#> gv[*foo] s 6 <1> entersub[t3] sKS/TARG,1 7 <2> add[t4] sK/2 8 <#> gvsv[*c] s 9 <2> sassign vKS/2 a <@> leave[1 ref] vKP/REFC -e syntax OK

leaves the sub as a sub since it doesn't have a null prototype list.

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Replies are listed 'Best First'.
Re^3: OOP - Constant Vs. Subroutine ()
by phaylon (Curate) on May 12, 2007 at 17:38 UTC

    While I have no idea of Perl's internals, it also seems that it depends what the sub contains. Of course, this is logical, since you can't inline everything. Seems to be that things that can be reduced to constant values will be inlined, which also makes it easier to remember :) So, "4" will be inlined, "4+4" too, since it can also be optimized away. "@INC" won't, for example.


    Ordinary morality is for ordinary people. -- Aleister Crowley