问题描述:

Say I am debugging a Perl script where the script was invoked as follows:

perl -d test.pl > file.txt

with test.pl:

print "Hello world\n";

my $a = 2;

print "$a\n";

1;

Is there any way to re-redirect the output of the script from within the debugger to the debugger stdout so that the print statements send their output on the scroll window of the debugger?

If not, is there any way to issue a command from within the debugger to flush out everything so far to file.txt?

网友答案:

You can evaluate arbitrary Perl while debugging, and DB::OUT is the filehandle opened by the debugger for output. So just use select DB::OUT:

Given test:

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

Here's a log demonstrating the use of 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
网友答案:

Although @Julian Fondren works, there is a minor change necessary to work remote.

given test:

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

Start a listener on whatever host and port on terminal 1 (here localhost:12345):

$ nc -v -l localhost -p 12345

for readline support use rlwrap (you can use on perl -d too):

$ rlwrap nc -v -l localhost -p 12345

And start the test on another terminal (say terminal 2):

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

Input/Output on terminal 1:

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> 

Output on terminal 2:

1

Note the dollar on

select $DB::OUT

NOTE: I don't mind if someone explain what magic is behind that change, but don't ask me because I don't know.

相关阅读:
Top