sub is_balanced { my $_ = shift; my $c = 0; while (/([()])/g) { if ($1 eq '(') {$c++; next} return if --$c < 0; } $c == 0; }