Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^2: OOP - Constant Vs. Subroutine ()

by shmem (Chancellor)
on May 12, 2007 at 09:25 UTC ( [id://615054]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-16 04:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found