[英]How to debug perl within a bash wrapper
我正在調試由bash腳本啟動的Perl程序。 由於Perl腳本包含大量的Perl模塊,並且需要提供極其復雜的選項,因此這里的bash包裝器絕對是必需的。
由於這種限制,我無法使用Perl調試器,這是我猜到的最簡單的方法。 然后我轉向舊的好的printf
。 但是,即使我將printf
s從一個添加到不同模塊中的另一個位置,實際上也不會打印任何內容到啟動bash包裝器的終端。
因此,我希望您首先解釋為什么我無法從內部Perl腳本中獲取任何打印信息,以及在這種情況下如何解決我的問題以調試Perl程序。
解釋為什么我無法從內部Perl腳本中獲取任何打印信息
大概您的bash腳本吞沒了輸出。
找出bash的功能,然后使用相同的參數和環境變量直接調用perl。
應該可以將STDOUT
和STDERR
重定向到文件:
# Inside the bash script
perl myScript.pl 1>perl_log.out 2>perl_log.err
滾動一個子項以打印調試輸出:
use constant debug_flag => 1; # At top of script(s)
{ my $debug_handle;
sub debug {
return unless debug_active; # Only prints debug msgs if debug active
unless ( $debug_handle ) { # Initialize debug log
open $debug_handle, '>', 'perl_log.out' or die $!;
}
print $debug_handle @_, "\n";
}
}
# call with 'debug'
debug ( 'Array length : ', scalar @array );
use Log::Log4perl; # Wonderful logging module
和
use Carp;
local $SIG{__DIE__} = \&Carp::confess; # Print out stack traces...
local $SIG{__WARN__} = \&Carp::cluck; # ... when using die and warn
最好盡可能避免使用printf
而改用print
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.