中
当我们在DEBUG模式下执行脚本时,是否可以禁用特定子例程的执行?。
Supoose,sub-tryme正在被调用,执行需要很长时间,我想禁用/跳过执行该子例程。
- 一个可用的选项是对调用进行注释-不建议编辑脚本
- 修改tryme()中检查的变量-子程序没有该功能
- 因此,我们可以使用任何DEBUG选项来禁用执行子例程吗
谢谢,
您可以设置全局变量或命令行变量来设置(例如)$debug = 1
。然后你可以这样指定你的子调用:
_long_function() unless $debug == 1;
或
unless ($debug) {
...
}
$^P
变量包含确定当前处于活动调试模式的标志。因此,我们可以编写在调试器中显示完全不同行为的代码:
$ cat heisenbug.pl
use List::Util qw/sum/;
if ($^P) {
print "You are in the debugger. Flags are ", unpack("b*", $^P), "n";
} else {
print "sum = ", sum(@ARGV), "n";
}
$ perl heisenbug.pl 1 2 3 4 5 6 7 8 9 10
sum = 55
$ perl -d heisenbug.pl 1 2 3 4 5 6 7 8 9 10
Loading DB routines from perl5db.pl version 1.37
Editor support available.
Enter h or 'h h' for help, or 'man perldebug' for more help.
main::(-:2): if ($^P) {
DB<1> n
main::(-:3): print "You are in the debugger. Flags are ", unpack("b*", $^P), "n";
DB<1> n
You are in the debugger. Flags are 10001100000111001010110010101100
Debugged program terminated. Use q to quit or R to restart,
use o inhibit_exit to avoid stopping after program termination,
h q, h R or h o to get additional info.
DB<1> q
$
变量和标志的含义记录在perlvar
您可以检查这样的环境变量:
_long_function() if $ENV{ DEBUG };
如果您想执行这个_long_function
,请运行如下脚本:
DEBUG=1 ./script.pl
在正常情况下,不会调用_long_function
:
./script.pl