Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: replacing literals with constants (empty prototype needed)

by LanX (Saint)
on May 14, 2022 at 09:25 UTC ( [id://11143896]=note: print w/replies, xml ) Need Help??


in reply to replacing literals with constants

First of all, this ...
  • sub ten {10}
is not a constant.

For constant folding (optimization at compile time) to happen, you need a a prototype of () , to make sure the result doesn't depend on input.

  • sub ten() {10}
Otherwise arguments are allowed, which is biting you here!

The following demos with B::Deparse will demonstrate what is happening

C:\tmp>perl -MO=Deparse,-p -e"sub ten {10}; print ten() - 1" sub ten { 10; } print((ten() - 1)); # <-- +- call no args -e syntax OK C:\tmp>perl -MO=Deparse,-p -e"sub ten {10}; print ten - 1" sub ten { 10; } print(ten((-1))); # <-- +- call with args -e syntax OK C:\tmp>perl -MO=Deparse,-p -e"sub ten() {10}; print ten - 1" sub ten () { 10; } print(9); # <-- +- constant folding -e syntax OK C:\tmp>perl -MO=Deparse,-p -e"sub ten {10}; 9 .. ten - 1" sub ten { 10; } (9 .. ten((-1))); # <-- +- not what you wanted -e syntax OK C:\tmp>

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

updates

  • s/code folding/constant folding/g
  • Log In?
    Username:
    Password:

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

    How do I use this?Last hourOther CB clients
    Other Users?
    Others exploiting the Monastery: (5)
    As of 2024-04-25 13:07 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found