[Melbourne-pm] Caller depth

leif.eriksen@hpa.com.au leif.eriksen at hpa.com.au
Tue Nov 22 19:13:47 PST 2005

Well it seems that there isn't a really obvious solution.

Nothing on PerlMonks oddly.

What you want (I believe) is the depth of the internal "markstack" -
however to do that means writing some XS code. Bleah.

Perhaps one of the Devel:: modules may help. Try Devel::DumpStack,
Devel::TraceFuncs or Devel::Peek. There may be some help also in the DB
package too.

Failing in all of those....

Alternatively, you could create an iterator over caller(), that returns 
1.undef when there are no more levels.
2. A nicely indented string for each level

Note following code is completely untested - just a pseudo-code brain
Much work is required to actually make the idea work, but I have a
conference call now...

# dump upto some (global?) depth
my $callStackIter = getCallStackIterator(maxDepth => $::MAXSTACK);

while (defined (my $callStackDump = $callStackIter->())) {
	print TRACELOG $callStackDump;

sub getCallStackIterator {
  my (%args) = @_;
  my $depth = 1; # don't trace ourselves ?
  return sub {
    return if $depth++ > $args{maxDepth};
    return formatStackDump($depth, caller($depth));

sub formatStackDump {
  my ($currentDepth, @stackDump) = @_;

  return sprint("%*s --> %s\n",
                  $currentDepth, # force field width
                  ' ',           # value of first %s
                  $stackDump[3]); # fully qualified function name

Again - this code almost certainly doesn't work, but the idea is to
localise the depth to each instance of the iterator. An advantage is
that different iterators will maintain their own depth localisers, so
you don't have to.

The main problem (apart from it not working) with the above is to skip
the stack frames related to dumping the caller stack itself - if that's
important to you.

Now to be completely embarrassed by the complete obvious solution.

-----Original Message-----
Is there any known way to get the 'depth' 
of the caller without counting?
The contents of this e-mail and its attachments are confidential and intended
solely for the use of the individual or entity to whom they are addressed.  If
you received this e-mail in error, please notify the HPA Postmaster, postmaster at hpa.com.au,
then delete  the e-mail.
This footnote also confirms that this e-mail message has been swept for the
presence of computer viruses by Ironport. Before opening or using any
attachments, check them for viruses and defects.
Our liability is limited to resupplying any affected attachments.
HPA collects personal information to provide and market our services. For more
information about use, disclosure and access see our Privacy Policy at

More information about the Melbourne-pm mailing list