sub transform_arguments { # We also want to handle arbitrarily deeply nested balanced parentheses here no warnings 'uninitialized'; s{\bsub(\s*) #1 (\w*) #2 (\s*) #3 \( (\s*) #4 ( #5 ( #6 (?: \\. # regex escapes and references | \( (?6)? # recurse for parentheses \) | \{ (?6)? # recurse for curly brackets \} | (?>[^\\\(\)\{\}]+) # other stuff )+ )* \@? # optional slurpy discard argument at the end ) (\s*)\) (\s*)\{}{ parse_argument_list("$2","$5","$1$3$4$8$9") }mgex; $_ }