tags:

views:

258

answers:

4

I'm using ActivePerl 5.8 on Windows XP.

use strict;
use warnings;
use Data::Dumper;

There are three subroutines used in my script.

To detect the call stack, I can only insert some print "some location"; and check the print result from console Window.

Is there any good method to monitor it? Thank you.

+8  A: 

Use the debugger's T command.

Example:

$ 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
daxim
+7  A: 

If it's your code, you might want to use:

Carp::cluck( "And here's the stack:" );

See Carp::cluck. It prints out a warning with a stack trace. It works like the "printf" style of debug output.

Axeman
+3  A: 

You weren't specific about why you'd like to monitor the call stack and trace your subs, so answers will have to be broad.

One method is caller:

caller

Returns the context of the current subroutine call. In scalar context, returns the caller's package name if there is a caller, that is, if we're in a subroutine or eval or require, and the undefined value otherwise. In list context, returns

# 0         1          2
($package, $filename, $line) = caller;

With EXPR, it returns some extra information that the debugger uses to print a stack trace. The value of EXPR indicates how many call frames to go back before the current one.

#  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);

You might also use the Devel::Cover module:

Code coverage data are collected using a pluggable runops function which counts how many times each op is executed. These data are then mapped back to reality using the B compiler modules. There is also a statement profiling facility which needs a better backend to be really useful.

The more you tell us about what you want to do, the more helpful to you our answers will be!

Greg Bacon
A: 

You rarely need to directly manage the call stack in Perl. If you do caller is the tool you want. However, it is only rarely needed.

More often, I want to see a stack trace when I am debugging. Good news, its easy to get a stack trace, simply use Carp's confess and cluck functions instead of die and warn.

use strict;
use warnings;
use Carp;

bar(6.1);
bar(1);

sub foo {
    confess "Oh noes" unless @_ == 6;  # confess is fatal
}

sub bar {
    my $count = shift;
    cluck "bar is in trouble" unless int $count == $count;  # cluck is not fatal
    foo( ('a')x $count );
}

This gets you:

dao:~ toad$ perl test.pl
bar is in trouble at test.pl line 14
    main::bar(6.1) called at test.pl line 5
Oh noes at test.pl line 9
    main::foo('a') called at test.pl line 15
    main::bar(1) called at test.pl line 6
daotoad