Better Living Through Thinking |
|
Subroutine TracingThu, 05 Oct 2006Sometimes we want to see the call chain of a subroutine, just to see where our data has been. For example, a program: blech(); exit; might have this kind of chain: blech() -> baz() -> bar() -> foo() Thus: sub blech { baz() }
sub baz { bar() }
sub bar { foo() }
sub foo { print "I'm in foo!" }
We add a little subroutine to help us trace this call chain: sub call_trace {
my ($pack, $sub);
my $i = 1;
do { ($pack, $sub) = (caller($i++))[0,3];
warn ' ' x $i . $sub . "\n" if $sub;
} while defined $sub;
}
And modify our final subroutine: sub foo { print "I'm in foo!"; call_trace() }
Now we get a nice bit of output: I'm in foo!
main::foo
main::bar
main::baz
main::blech
|
Audio Broadcast(standby)Moon StatusPhase: 99.97%Illuminated: 0.00% Age (days): 29.52
Sun May 20 17:36:09 MDT 2012 |