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: 
The scalar returned by `$n` is stored on the stack.

This is what the stack looks like just before calling `fact`:

* Scalar returned by `$n` in recursion level 0
* List:
	+ Scalar returned by `$n` in recursion level 1
	+ List:
		- Scalar returned by `$n` in recursion level 2
		- List:
			* Scalar returned by `$n-1` in recursion level 2
			* Scalar returned by `\&fact` in recursion level 2

This is what the stack looks like just after calling `fact`:

* Scalar returned by `$n` in recursion level 0
* List:
	+ Scalar returned by `$n` in recursion level 1
	+ List:
		- Scalar returned by `$n` in recursion level 2
		- Scalar returned by `fact($n - 1)` in recursion level 2

At this point, the multiplication operator will multiply the last two values on the stack, and place the result on the stack.

* Scalar returned by `$n` in recursion level 0
* List:
	+ Scalar returned by `$n` in recursion level 1
	+ List:
		- Scalar returned by `$n * fact($n - 1)` in recursion level 2

Then the sub returns,

* Scalar returned by `$n` in recursion level 0
* List:
	+ Scalar returned by `$n` in recursion level 1
	+ Scalar returned by `fact($n - 1)` in recursion level 1

And so on.
Intermediate results of any call are kept on the stack, just like local variables within a function.

```
return $n * fact($n - 1);

```

is processed equivalently to:

```
my $temp = fact($n - 1);
return $n * $temp;

```

UPDATE: I see you're also interested in where the product is held before returning it. That's also in a temporary on the stack, so it's equivalent to:

```
my $temp1 = fact($n - 1);
my $temp2 = $n * $temp1;
return $temp2;

```