How can I control the Perl call stack?

I am using ActivePerl 5.8 on Windows XP.

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

There are three routines in my script.

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

Is there any good way to control it? Thanks.

+5
source share
4 answers

You were not specifically about why you want to keep track of the call stack and keep track of your subtitles, so the answers should be broad.

One method caller:

subscriber

. , , , eval require, undefined .

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

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

Devel:: Cover:

, , . B. , , .

, , !

+5

, :

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

. Carp::cluck. . printf.

+10

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
+9

Perl. caller, , . .

Most often, I want to see the stack trace during debugging. Good news, it's easy to get a stack trace, just use Carp confess and cluckinstead of dieand 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 will help 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
0
source

All Articles