web-dev-qa-db-ja.com

Perlで呼び出しスタックのリストを取得するにはどうすればよいですか?

Perlスクリプトの現在の位置に先行するサブコールの任意の深さまでサブ+モジュールのリストに(印刷のために)アクセスできる方法はありますか?

一部のPerlモジュール(.pm)に変更を加える必要があります。ワークフローはcgiスクリプトを介してWebページから開始され、データを使用する必要があるモジュールで終わるいくつかのモジュール/オブジェクトに入力を渡します。線に沿ってどこかでデータが変更されたので、どこを見つける必要があります。

61
slashmais

Devel :: StackTrace を使用できます。

use Devel::StackTrace;
my $trace = Devel::StackTrace->new;
print $trace->as_string; # like carp

Carpのトレースのように動作しますが、フレームをより細かく制御できます。

1つの問題は、参照が文字列化されており、参照値が変更されても表示されないことです。ただし、 PadWalker を使用して完全なデータを印刷することもできます(ただし、非常に大きくなります)。

59
Ovid

caller でできますが、それ以上の情報が必要な場合もあります。

18
Leon Timmermans

Carp::longmessはあなたが望むことをするでしょう、そしてそれは標準です。

use Carp qw<longmess>;
use Data::Dumper;
sub A { &B; }
sub B { &C; }
sub C { &D; }
sub D { &E; }

sub E { 
    # Uncomment below if you want to see the place in E
    # local $Carp::CarpLevel = -1; 
    my $mess = longmess();
    print Dumper( $mess );
}

A();
__END__
$VAR1 = ' at - line 14
    main::D called at - line 12
    main::C called at - line 10
    main::B called at - line 8
    main::A() called at - line 23
';

私はこのサブを思いつきました(オプションのblessin 'アクションが追加されました!)

my $stack_frame_re = qr{
    ^                # Beginning of line
    \s*              # Any number of spaces
    ( [\w:]+ )       # Package + sub
    (?: [(] ( .*? ) [)] )? # Anything between two parens
    \s+              # At least one space
    called [ ] at    # "called" followed by a single space
    \s+ ( \S+ ) \s+  # Spaces surrounding at least one non-space character
    line [ ] (\d+)   # line designation
}x;

sub get_stack {
    my @lines = split /\s*\n\s*/, longmess;
    shift @lines;
    my @frames
        = map { 
              my ( $sub_name, $arg_str, $file, $line ) = /$stack_frame_re/;
              my $ref =  { sub_name => $sub_name
                         , args     => [ map { s/^'//; s/'$//; $_ } 
                                         split /\s*,\s*/, $arg_str 
                                       ]
                         , file     => $file
                         , line     => $line 
                         };
              bless $ref, $_[0] if @_;
              $ref
          } 
          @lines
       ;
    return wantarray ? @frames : \@frames;
}
18
Axeman

このコードは追加モジュールなしで動作します。必要な場所に含めるだけです。

my $i = 1;
print STDERR "Stack Trace:\n";
while ( (my @call_details = (caller($i++))) ){
    print STDERR $call_details[1].":".$call_details[2]." in function ".$call_details[3]."\n";
}
17
Thariama

Carp::confessCarp::cluckもあります。

16
jkramer

よりきれいなもの: Devel :: PrettyTrace

use Devel::PrettyTrace;
bt;
3
user2291758

非コアモジュールを使用できない(または回避したい)場合に、私が思いついた簡単なサブルーチンを以下に示します。

#!/usr/bin/Perl
use strict;
use warnings;

sub printstack {
    my ($package, $filename, $line, $subroutine, $hasargs, $wantarray, $evaltext, $is_require, $hints, $bitmask, $hinthash);
    my $i = 1;
    my @r;
    while (@r = caller($i)) {
        ($package, $filename, $line, $subroutine, $hasargs, $wantarray, $evaltext, $is_require, $hints, $bitmask, $hinthash) = @r;
        print "$filename:$line $subroutine\n";
        $i++;
    }
}

sub i {
    printstack();
}

sub h {
    i;
}

sub g {
    h;
}

g;

次のような出力を生成します。

/root/_/1.pl:21 main::i
/root/_/1.pl:25 main::h
/root/_/1.pl:28 main::g

またはoneliner:

for (my $i = 0; my @r = caller($i); $i++) { print "$r[1]:$r[2] $r[3]\n"; }

呼び出し元 こちら のドキュメントを見つけることができます。

1
x-yuri