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

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

Replies are listed 'Best First'.
Re^3: Macro in perl code?
by shmem (Chancellor) on Oct 16, 2007 at 20:58 UTC
    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!
        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}