You could always take the regex you wish to return on as a secondary parameter. The rub is that you would have to manage regexes outside of this function and create a more generic die message.
Also if you wish to make this some sort of alt logic recursive method, there are better ways to get out of the recurse loop than a die.
Another options is to generate the anonymous sub routines to fit the mode you wish to execute. There are a variety of ways to do what I think you want to do. This solution could be recursive or operate over a queue (list) of generated subs (non-recursive).