Without having tested this, could it be that $1 being a global variable, is continously overwritten in each next iteration of this recursive subroutine, until the last step where $1 becomes undef due to the regex failing? That undef value then "trickles" back up the recursive subs and gives this error.
in reply to $1 not "freezing" in an addition
By "interpolating"/"quoting" the $1 perhaps the acctual value of $1 at that stage in the recursion gets indeed saved somehow.
It is always a good idea to immediately save those special variables (such as $_, @_, $1, ...) into a lexical variable to avoid these pitfalls. We tend to forget that these are global variables that are therefore prone to "action at a distance".
A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James
My blog: Imperial Deltronics