从Perl脚本中重新将输出重定向到stdout

时间:2013-05-06 19:00:14

标签: perl debugging

假设我正在调试调用脚本的Perl脚本,如下所示:

perl -d  test.pl > file.txt

test.pl

print "Hello world\n";
my $a = 2;
print "$a\n";
1;

是否有任何方法可以将重新定向脚本的输出从调试器内部转移到调试器标准输出,以便print语句在滚动窗口上发送输出调试器?

如果没有,有没有办法从调试器内发出命令清除到目前为止file.txt的所有内容?

2 个答案:

答案 0 :(得分:2)

您可以在调试时评估任意Perl,DB :: OUT是调试器为输出打开的文件句柄。所以只需使用select DB::OUT

鉴于测试:

use v5.14;
say 1;
say 2;
say 3;

这是一个显示select:

使用的日志
$ perl -d test > log

Loading DB routines from perl5db.pl version 1.33
Editor support available.

Enter h or `h h' for help, or `man perldebug' for more help.

main::(test:2):     say 1;
  DB<1> n
main::(test:3):     say 2;
  DB<1> select DB::OUT

  DB<2> n
2
main::(test:4):     say 3;
  DB<2> n
3
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<2> q
$ cat log
1

答案 1 :(得分:1)

虽然@Julian Fondren工作,但远程工作需要进行微小的改动。

给出测试:

Human

在终端1上的任何主机和端口上启动监听器(此处为localhost:12345):

use v5.14;
say 1;
say 2;
say 3;

使用rlwrap获取readline支持(您也可以在$ nc -v -l localhost -p 12345 上使用):

perl -d

然后在另一个终端(比如终端2)上开始测试:

$ rlwrap nc -v -l localhost -p 12345

终端1上的输入/输出:

$ PERLDB_OPTS="RemotePort=localhost:12345" perl -d test

终端2上的输出:

Connection from 127.0.0.1:42994

Loading DB routines from perl5db.pl version 1.49
Editor support available.

Enter h or 'h h' for help, or 'man perldebug' for more help.

main::(test:2): say 1;
  DB<1> n
main::(test:3): say 2;
  DB<1> select $DB::OUT

  DB<2> n
2
main::(test:4): say 3;
  DB<2> n
3
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<2> 

注意美元

1

注意:如果有人解释这种变化背后的魔力,我不介意,但不要问我,因为我不知道。