2013-07-23 40 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 
相關問題