use Text::Balanced qw( extract_tagged ); # The rest of your code from above here.