$text = "A(B(2)+C(D())),\nTT(TT()+TT(TT()))"; $text = doFunc( "MAIN", $text ); # print "Example: $text\n"; sub doFunc{ my( $theFunc, $theParam ) = @_; # FIXME: greedy match fails with: 'TT(TT()+TT(TT()))' my $re; {local $^W=0; ($re=$theParam)=~s/((\()|(\))|.)/${[')']}[!$3]\Q$1\E${['(']}[!$2]/gs; $re= join'|',map{quotemeta}eval{$theParam=~/$re/};} die $@ if $@=~/unmatched/; $theParam =~ s/([A-Z]+)\(($re)\)/&doFunc($1,$2)/geo; my $result = ""; if( $theFunc eq "MAIN" ) { $result = $theParam; } else { # dummy switch here for demonstration # this would be an elsif for each function $result = "func $theFunc returns <$theParam>"; } return $result; }