extract_bracketed($code, '{}') to handle this, but even that will still bomb on curlies in comments:

sub blah { # } is such a neat character return '}'; }