Beware of the side effects of the Perl built-in variables, your solution is not likely going to work as you expected, the $_ will get clobberred in your recursion and you will sit there scratching your head for hours trying to find out what's going wrong. :-)