in reply to Getting size of call stack

Increment the depth until you get nothing back.

Update 1: Oops. I accidentally declared $depth twice.
Update 2: I accidentally put a semi-colon after `my depth = 0` which screws up the parameters for the for(;;). Replacing it with a comma keeps the correct meaning.

my @stack; for ( my $depth = 0, my $frame = [ caller $depth ]; scalar( @$frame ); $depth++ ) { push @stack, $frame; }

Replies are listed 'Best First'.
Re^2: Getting size of call stack
by aufflick (Deacon) on Jan 28, 2005 at 05:05 UTC
    I like that you have both the stack to inspect (eg. dump it to stderr) and the stack depth (scalar(@stack))

    For my liking though, this is more intuitive:

    my @stack; my $depth = 0; while (my @frame = caller $depth++) { push @stack, \@frame[0,1,2]; }
      The entire purpose of the for(;;) loop is to handle preinitializations and temporary variables like your my $depth = 0 and to separate your test scalar( @frame ) from your NEXT() in $depth++.
        I guess I've never like shoe-horning code inside the c-like for operator.

        There's also no need to keep track of a seperate $depth since it's the same as scalar(@stack).

        My version seems easier to read and more descriptive of what's actually happening. That's just a personal preference though...