Perlでコールスタック上のサブルーチンの引数を得る
@DB::args
でできます。
use strict; use warnings; use Data::Dumper; sub foo { bar(1, 2, 3) } sub bar { package DB { our @args; my $i = 0; while (() = caller $i) { print "$i: ", ::Dumper \@args; $i++; } } } foo('a', 'b', 'c');
outout:
0: $VAR1 = [ 1, 2, 3 ]; 1: $VAR1 = [ 'a', 'b', 'c' ];
もともとデバッガAPIの一部なので使い方はかなりマジカルで、DBパッケージ内でcaller()に引数を与えてかつリストコンテキストで評価すると@DB::argsにそのコールスタックでの引数がセットされるというものになっています。
以下perldoc -f callerより
Furthermore, when called from within the DB package in list context, and with an argument, caller returns more detailed information: it sets the list variable @DB::args to be the arguments with which the subroutine was invoked.
自分で呼び出すことはあまりないのですが、CarpモジュールなどはこのAPIを使っているのでconfess()などで出すスタックトレースには引数が含まれていてデバッグが容易だったりします。