|
|
| Perl-Sensitive Sunglasses | |
| PerlMonks |
Re: Getting the size of the call stack (efficiently)by grinder (Bishop) |
| on Feb 27, 2007 at 18:42 UTC ( [id://602368]=note: print w/replies, xml ) | Need Help?? |
|
What's so inefficient about it? The fact that there appears to be a lot of make-work code? I think you'll find that there are already modules available which will do what you want, Devel::StackTrace and Devel::CallerItem spring to mind, but I suspect they call caller under the covers themselves. You could use them as building blocks, and then there wouldn't be as much visible caller-frame-grovelling code in your own code. Or you could turn the problem on its head and use Hook::WrapSub to track your caller stack yourself, by writing push and pop wrappers that maintain a stack, around the functions you're interested in tracking. Apart from that, I'm not surprised you find your code as shown to be slow... it has an infinite loop! update: err, no you're right it hasn't. Nonetheless, C-style for loops are so rare in Perl that I probably won't be the only person who gets confused by it (... I was looking for a last somewhere I guess).
update: I didn't realise at first that you only cared about the depth, not what's at each level. In which case, the following snippet will be pretty efficient (since it is blockless):
I wouldn't worry about it being too slow, and least not right away. Make it readable first. • another intruder with the mooring in the heart of the Perl
In Section
Seekers of Perl Wisdom
|
|
||||||||||||||||||||||||||||||||||