如何监控Perl调用堆栈?

Nan*_* HE 5 perl callstack

我在Windows XP上使用ActivePerl 5.8.

use strict;
use warnings;
use Data::Dumper;
Run Code Online (Sandbox Code Playgroud)

我的脚本中使用了三个子例程.

要检测调用堆栈,我只能插入一些print "some location";并从控制台窗口检查打印结果.

有什么好方法可以监控它吗?谢谢.

Axe*_*man 10

如果是您的代码,您可能希望使用:

Carp::cluck( "And here's the stack:" );
Run Code Online (Sandbox Code Playgroud)

Carp::cluck.它打印出带有堆栈跟踪的警告.它的工作方式类似于"printf"样式的调试输出.


dax*_*xim 9

使用调试器的T命令.

例:

$ perl -d -e'
sub foo {}
sub bar { foo; }
bar;
'

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

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

main::(-e:4):   bar;
  DB<1> s
main::bar(-e:3):        sub bar { foo; }
  DB<1> s
main::foo(-e:2):        sub foo {}
  DB<1> T
. = main::foo() called from -e line 3
. = main::bar() called from -e line 4
  DB<1> s
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<1> q
Run Code Online (Sandbox Code Playgroud)


Gre*_*con 5

您没有具体说明为什么要监视调用堆栈并跟踪您的潜艇,因此答案必须是广泛的.

一种方法是caller:

呼叫者

返回当前子例程调用的上下文.在标量上下文中,如果存在调用者,则返回调用者的包名称,即,如果我们处于子例程或eval或require中,则返回未定义的值.在列表上下文中,返回

# 0         1          2
($package, $filename, $line) = caller;
Run Code Online (Sandbox Code Playgroud)

使用EXPR,它返回一些调试器用于打印堆栈跟踪的额外信息.EXPR的值表示在当前呼叫帧之前返回的呼叫帧数.

#  0         1          2      3            4
  ($package, $filename, $line, $subroutine, $hasargs,
#  5          6          7            8       9         10
$wantarray, $evaltext, $is_require, $hints, $bitmask, $hinthash)
 = caller($i);
Run Code Online (Sandbox Code Playgroud)

您也可以使用Devel :: Cover模块:

使用可插入的runops函数收集代码覆盖率数据,该函数计算每个op执行的次数.然后使用B编译器模块将这些数据映射回现实.还有一个语句分析工具,需要更好的后端才能真正有用.

你告诉我们你想做什么越多,我们的答案就越有帮助!