Imagine if Perl 5's caller
returned an object which represented the call chain to the point of the
object's creation.
I want to inspect the call stack within a helper module, but I don't care about the call stack within the module. I want to use lots of little helper functions, because that's good design, but caller
works against that. Looking up the stack means keeping track of the magic number of call frames within my module I currently use, and that's one more thing to update when I change things.
That's structural code highly coupled to the arrangement of other code, and if that doesn't wrinkle your nose with the subtle aroma of fragility and peril, I don't know what will.
Imagine if instead:
my $caller_object = capture_caller_state();
while (my $call_frame = $caller_object->next)
{
next if $call_frame->is_eval;
say $call_frame->location_as_string;
my $next_frame = $call_frame->previous;
...
}
Imagine if you could pass this object around.
I know things get complicated if you pass this object up the call chain, but stack unwinding is a solved problem in that anyone capable of recognizing the problem should be able to figure out cheap and easy ways to fix it.
Alas, Perl 5.14 doesn't have this feature, and it's probably too late for 5.16 to get it, so for today I'm stuck imagining what might be.
(If you've never thought about this sort of thing before, you owe it to yourself to learn more about Continuation Passing Style, which is at least an order of magnitude more mind-bending at the start and at least two orders of magnitude more useful.)
This seems to be more or less exactly what Devel::StackTrace provides.
Imagine if you could goto the stack object, to effect a return from the subroutine at any point.