in reply to Re^2: Macro in perl code?
in thread Macro in perl code?

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}

Replies are listed 'Best First'.
Re^4: Macro in perl code?
by pileofrogs (Priest) on Oct 16, 2007 at 23:45 UTC

    You Rule!

Re^4: Macro in perl code?
by Krambambuli (Curate) on Oct 17, 2007 at 07:18 UTC
    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!
      sub X() { 0 } print "$message1 \n" if( runtime cond);

      Erm, no. The sub and the print statement being on the same line is an artifact of the code being a oneliner :-)

      The point is that

      sub X() { 0 }

      is folded into a constant at compile time (more accurate after compiling - while optimizing), and the resulting constant is used by the optimizer to further reduce the optree throwing away code which, depending on that constant, is never reached.

      So, it's not a (runtime cond). You get a mandatory warning if you redefine the constant defining sub X() { } at runtime, and you won't get back code that has been optimized away after compile.

      I assumed that since the OP did preprocessing with m4 even before the perl compiler sees the code, the value for his $verbose doesn't change at runtime, but I may be mistaken. Conditional code that depends (or could depend) on runtime state can't be optimized that way, so there's a difference in writing

      { my $debug = 0; if ($debug) { warn "debug set\n"; } }

      and

      { sub debug () { 0 } if (debug) { warn "debug set\n"; } }

      Only in the latter case the if() block is optimized away, even if, in the former, there's no place in the scope in which the my $debug is declared, where its value would change.

      qwurx [shmem] ~ > perl -MO=Deparse,-x,-p { my $debug = 0; if ($debug) { warn "debug set\n"; } } __END__ { (my $debug = 0); if ($debug) { warn("debug set\n"); } } __DATA__ (<Ctrl>D pressed) - syntax OK qwurx [shmem] ~ > perl -MO=Deparse,-x,-p { sub debug () { 0 } if (debug) { warn "debug set\n"; } } __END__ sub debug () { 0 } { '???'; } __DATA__ (<Ctrl>D pressed) - syntax OK

      --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}
        Thank you. It took some time, but now [thanks god, finally...! :) ] I got it.

        Ah.. no wonder I couldn't make it work...

        I am using a runtime condition. The idea was rather than call a function which evaluates the cond and may simply exit, put the code inline and avoid the function call overhead.