http://www.perlmonks.org?node_id=45447


in reply to debugging /logging help

A brief comment on efficiency:

If you use constant subs instead of variables for toggling debugging, Perl is smart enough to compile them out of existence so when you go into production the debugging will have no adverse performance effects.

You can observe this in action using B::Deparse:

[~] $ perl -MO=Deparse -e'sub D(){0}print"foo"if D' sub D () { 0; } '???'; -e syntax OK [~] $ perl -MO=Deparse -e'sub D(){1}print"foo"if D' sub D () { 1; } print 'foo'; -e syntax OK

The `???' in the first example means that Perl has optimized out whatever used to be there. And you'll note the lack of a conditional in the second example, which was also optimized out. Perl is smart enough to realize that a subroutine with no parameters can be computed at compile time, and so replaces all calls to D() with its actual value (0 or 1 depending on the example). Then the peephole optimizer realizes that the print statements depend on the conditionals, and since the value of the conditionals is known, eliminates them and leaves either nothing, in the case of D() == 0, or just the bare print, in the case of D() == 1.

-dlc