在Perl的DEBUG模式下禁用执行子程序

3

当我们在DEBUG模式下执行脚本时,是否有可能禁用特定子程序的执行?

假设正在调用sub tryme,需要执行很长时间,我希望能够禁用/跳过执行该子程序。

  • 一种可行的选择是注释调用-不建议编辑脚本
  • 修改一个变量,该变量在tryme()中进行检查-子例程没有这个功能
  • 因此,我们可以使用任何DEBUG选项来禁用执行子程序吗?

谢谢,

3个回答

1
你可以设置一个全局变量或命令行变量来设置(例如)$debug = 1。然后你可以像这样指定你的子调用:
_long_function() unless $debug == 1;

或者

unless ($debug) {
    ...
}

0

您可以像这样检查环境变量:

_long_function()   if $ENV{ DEBUG };

如果你想要执行_long_function,可以像下面这样运行脚本:

DEBUG=1 ./script.pl

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

./script.pl

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中有记录。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接