in reply to Re^3: Refactoring challenge.
in thread Refactoring challenge.

The challenge was to rewrite the code presented so that it will perform identically (modulus I/O) without knowing the context. :-)

As for $depth, it is scoped to the sub. As in only pp() can access or modify $depth. The reason it's not within the sub is that $depth (and $indent) are both what would be static variables in C/C++. They're persistent across calls to the subroutine, so must not be reinitialized every time pp() is called (which is what scoping them within the subroutine would do).

Now, you cannot do a complete refactoring to remove all smells, which I alluded to further down the thread. However, you can improve the code as it stands, knowing that some smells will remain. For example, f1() and f2() should be hidden behind a common interface which would return back $pod and $depth_mod.

Being right, does not endow the right to be rude; politeness costs nothing.
Being unknowing, is not the same as being stupid.
Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.