[英]Re-redirecting output to stdout from within Perl script
假設我正在調試調用腳本的Perl腳本,如下所示:
perl -d test.pl > file.txt
與test.pl
:
print "Hello world\n";
my $a = 2;
print "$a\n";
1;
有沒有辦法重新將腳本的輸出從調試器重定向到調試器標准輸出,以便print語句在調試器的滾動窗口上發送它們的輸出?
如果沒有,有沒有辦法從調試器中發出一個命令來清除目前為止所有的文件到file.txt
?
您可以在調試時評估任意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
盡管@Julian Fondren工作,但是遠程工作需要進行微小的改動。
給定測試:
use v5.14;
say 1;
say 2;
say 3;
在終端1上的任何主機和端口上啟動監聽器(此處為localhost:12345):
$ nc -v -l localhost -p 12345
對於readline支持使用rlwrap (你也可以在perl -d
上使用):
$ rlwrap nc -v -l localhost -p 12345
並在另一個終端(比如終端2)上開始測試:
$ PERLDB_OPTS="RemotePort=localhost:12345" perl -d test
終端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>
終端2的輸出:
1
注意美元
select $DB::OUT
注意:我不介意有人解釋這種變化背后的魔力,但不要問我,因為我不知道。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.