当我们在DEBUG模式下执行脚本时,是否可以禁用特定子例程的执行?
Supoose,sub tryme被调用并且需要很长时间才能执行,我想禁用/跳过执行子程序。
谢谢,
答案 0 :(得分:1)
您可以设置全局变量或命令行变量来设置(例如)$debug = 1
。然后你可以像这样指定你的子调用:
_long_function() unless $debug == 1;
或
unless ($debug) {
...
}
答案 1 :(得分:0)
我想你会在identify a procedure and replace it with a different procedure中找到你想要的东西。接受的答案应该涵盖它。
答案 2 :(得分:0)
$^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
$
中记录了变量和标志的含义
答案 3 :(得分:0)
您可以像这样检查环境变量:
_long_function() if $ENV{ DEBUG };
如果您希望执行此_long_function
,请运行下一个脚本:
DEBUG=1 ./script.pl
在正常情况下,不会调用_long_function
:
./script.pl