The problem is with the nature of the
each function. This will return keys of the hash in order, until it returns false when there are none left. Then it starts over again.
The problem now is that when you call you function recursively, the call to each isn't getting reset. So you go through the first three key/value pairs in the top function, then your subordinates search that you performed recursively is given the last key/value pair when it first evaluates each. Then, it gets
undef, which is why it exits prematurely. Now that the hash has returned undef, it starts from the beginnig with the original function again. The global nature of %boss ensures that your recursion doesn't work.
To stop this behavior, begin and end your function with
keys %boss This will ensure that you start anew with the values. If it is possible to have a structure where a person has more than one subordinate, like this:
$VAR1 = {
'4' => '3',
'1' => '1',
'3' => '2',
'2' => '1',
'3' => '5'
};
Then you will want to try assigning a temp hash before you call recursively. Though this isn't tested, you could try doing this at the top of your function:
my %temp_boss = %boss;
while (my ($employeeid, $bossid) = each %temp_boss) {
I think that recursive functions play nicely with lexical scoping, but don't quote me on that.
HTH.