in reply to Macro in perl code?

Use a subroutine with an empty prototype which return a constant (that's what the constant module does):
perl -MO=Deparse,-x,-p -e 'sub verbose() { 0 } print "guggug!\n" if ve +rbose;' sub verbose () { 0 } '???'; -e syntax OK
perl -MO=Deparse,-x,-p -e 'sub verbose() { 0 } if(verbose){ print "gug +gug!\n"};' sub verbose () { 0 } '???'; -e syntax OK

The '???'; statements stand for bits of code that have been optimized away; those will have no impact on performance. If the compiler sees such a constant returning sub it can perform optimizations, eliminating code blocks whose execution is bound to the value of that constant.

Besides, before shoehorning a perl script through m4, I'd rather look - though discouraged - at perl's -P switch. See perlrun.

--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^2: Macro in perl code?
by pileofrogs (Priest) on Oct 16, 2007 at 20:32 UTC

    Sorry, I don't get it at all. Could you point me to some docs that explain this? I think I need to get more background..

      Constant functions (with the empty prototype) are discussed in perlsub.
      Constant Functions Functions with a prototype of "()" are potential candidates for inlining. If the result after optimization and constant folding is either a constant or a lexically-scoped scalar which has no other references, then it will be used in place of function calls made without "&". Calls made using "&" are never inlined. (See con- stant.pm for an easy way to declare most constants.)

      See e.g. The Optimizer and Preprocessor Pranks for more details.

      --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}

        You Rule!

        Sorry for asking again, but I'm still unsure about how to use this. Could you explain/exemplify a bit more ?

        Would I have to have say
        ... sub X() { 0 } print "$message1 \n" if( runtime cond); ... sub X() { 0 } print "$message2 \n" if( runtime cond); ...
        ?!

        Why would that be better than
        ... print "$message1 \n" if( runtime cond); ... print "$message2 \n" if( runtime cond); ...
        I'm confused. Would be nice if you can shed a bit more light on it - thanks!