我正在调试由bash脚本启动的Perl程序。因为Perl脚本由大量的Perl模块组成,需要提供极其复杂的选项,所以这里的bash包装绝对是必要的。
由于这个限制,在我的情况下我不能使用Perl调试器,这是我猜的最简单的方法。然后我转向旧的printf
。但是,即使我将printf
从一个地方添加到不同模块中的另一个地方,实际上没有任何内容打印到我启动bash包装器的终端。
所以,我希望您首先解释为什么我无法从内部Perl脚本获取任何打印信息,以及如何解决我的问题以调试Perl程序。
答案 0 :(得分:4)
解释为什么我无法从内部perl脚本获取任何打印信息
据推测,你的bash脚本正在吞噬输出。
弄清楚bash在做什么,并使用相同的参数和环境变量直接调用perl。
答案 1 :(得分:0)
应该可以将STDOUT
和STDERR
重定向到file:
# 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