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


in reply to Debug code out of production systems

This is really easy to achive. perl will optimize away sections enclosed in if blocks with constant conditions:

$ perl -MO=Deparse -le ' > sub DEBUG () { 1 } > if(DEBUG) { print "Debug" } > print "Done"; > ' BEGIN { $/ = "\n"; $\ = "\n"; } do { print 'Debug' }; print 'Done'; -e syntax OK $ perl -MO=Deparse -le ' > sub DEBUG () { 0 } > if(DEBUG) { print "Debug" } > print "Done"; > ' BEGIN { $/ = "\n"; $\ = "\n"; } '???'; print 'Done'; -e syntax OK

So with a false constant, perl will replace the if with a statement B::Deparse doesn't like (but is probably a noop). With a true constant, it gets enclosed in a do block (in case there are any lexicals declared--perl doesn't look ahead far enough to know).

(And I say 'perl', not 'Perl', because it's possible that a different implementation of Perl, should there ever be one (Ponie?), may apply different optimizations.)

----
I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
-- Schemer

: () { :|:& };:

Note: All code is untested, unless otherwise stated