2013-07-23 42 views
3

当我们在DEBUG模式下执行脚本时,是否可以禁用特定子例程的执行?禁用在DEBUG模式下在Perl中执行子例程

Supoose,子tryme正在调用,需要相当长的时间来执行,我想禁用/跳过执行子例程。可

  • 一种选择是评论的电话 - 编辑脚本,不建议
  • 修改它在tryme检查一个变量() - 的子过程不具有设施
  • 因此,我们可以使用任何调试选项来禁用执行子程序

感谢,

回答

1

您可以设置一个全局变量或命令行变量设置(例如)$debug = 1。然后,你可以specifiy您的子呼叫这样的:

_long_function() unless $debug == 1; 

unless ($debug) { 
    ... 
} 
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 
$ 

变量和标志的含义在perlvar

0

记录您可以检查这样的环境变量:

_long_function() if $ENV{ DEBUG }; 

和运行脚本像旁边,如果你想这个_long_function执行:

DEBUG=1 ./script.pl 

在正常情况下,将不会调用_long_function

./script.pl 
相关问题