Question: There is a recursive implementation of computing factorial N on Perl.

```
sub fact {
  my ($n) = shift;
  return $n if $n <= 2;
  return $n * fact($n - 1);
}

```

Can someone explain me where Perl holds intermediate results before function gives a result?

**UPD** and how I can see them by using debugger or by use something else?

From answers I was explained that this values keeps in a stack but how I can see these values from the stack?

Answer: 
With the recursion any of the arguments that are passed in each call go on the call frame/ stack. By using Carp & cluck you can see the call frames. Intermediate results are calculated while the stack unwinds on reaching the base case ($v == 1). Could it be in CPU register only? And operator (\*) multiplies this intermediate result with $v that is on the stack. Also checkout [this article](http://www.hiregion.com/2013/02/recursion-defined.html).

```
#!/usr/bin/env  perl

use strict;
use IO::Handle;
use Carp qw(cluck);

STDOUT->autoflush(1);
STDERR->autoflush(1);

sub factorial {
    my $v = shift;

    dummy_func();
    return 1 if $v == 1;
    print "Variable v value: $v and it's address:", \$v, "\ncurrent sub factorial addr:", \&factorial, "\n","-"x40;
    return $v * factorial($v - 1);
}

sub dummy_func {
    cluck;
}

factorial(5);

```

Also running in debug mode will help.

> 
> perl -d factorial.pl
> 
> 
>
Since `$n` is declared as `my $n`, it's a lexically scoped variable and is stored in the stack and not in the system table. See [Perl Variables via my()](http://perldoc.perl.org/perlsub.html#Private-Variables-via-my%28%29) for more information.