I think this is not an easy task - it will not be possible without any significant revision of the coding style.
Approaches I can think of:
Put your debug code in a separate module.
Insert a warning into a module that indicates that it is enabled - embed in the "BEGIN" block. (This will not necessarily tell you which sub-call is called, and where)
eg.
BEGIN { carp "Debugging module is imported" }
The advantages of this may be: you can have a module "debug" and "not debug", as well as the same subtitles in both. "Not debugging" will be placed in dummy subtitles.
Then you can use B::Lint to check your module and check if subkeys are defined:
perl -MO=Lint[,OPTIONS] foo.pl
undefined-subs This option warns whenever an undefined subroutine is invoked. This option will only catch explicitly invoked subroutines such as foo() and not indirect invocations such as &$subref() or $obj->meth() . Note that some programs or modules delay definition of subs until runtime by means of the AUTOLOAD mechanism
Assign your substream via the link.
Edit: it doesnโt work as good as I thought - had the concept of declaring anonymous subscribers and creating links in BEGIN blocks - it doesnโt work as good as I thought - it is only runtime errors because you get errors in volume.
The best I can offer is:
use strict; use warnings; my $debug = 1; ##debug code bit. #Comment out if not needed my $debug_sub = gimme_debug(); sub gimme_debug { if ( $debug ) { warn "Debugging enabled\n"; return sub { print "Got values of :", @_, "\n"; }; } else { warn "Debug subroutine defined with debug disabled!" }; } #... way down code ... &$debug_sub ( "Some input" );
If you remove the declaration, you will get a compile-time warning because $debug_sub not declared. This can be combined with the above, for example, linking it in a module or object.
Automated Testing
It does not specifically do what you asked for, but you can still consider it. Automatic testing of your code / modules. For example, using TAP::Harness . This is a little more complicated and more intense than you ask, but having something that automatically delays the debugging code (and otherwise checks the various components of your code) is probably the โrightโ way to do this.
I also heard (but not really done much) Class::Inspector , which may be suitable for your needs. But I think that at a fairly fundamental level, what you are trying to do is not checked at compile time, because it is very difficult to evaluate which bits of code are actually called without actually running the code. So all you do is what you need to "manually manage."
"In computability theory, the stopping problem is the problem of determining, from a description of an arbitrary computer program and input, whether the program will end or continue to work forever.
Alan Turing proved in 1936 that a general algorithm for solving the stop problem for all possible pairs of program inputs cannot exist. A key part of the proof was the mathematical definition of a computer and a program that became known as a Turing machine; the stopping problem is unsolvable over Turing machines. This is one of the first examples of solving the problem.
So, if you come up with a good solution for this - perhaps a big win will be useful here :).