$in = s/^\((.*)\)$/$1/; my @splat = split(/,/, $in); my ( $depth, $left ); while ( my $c = shift @splat ) { $left .= ( $left ? ',' : '' ) . $c; ++$depth if index($c, '(') >= $[; --$depth if index($c, ')') >= $[; last if $depth == 0; } my $right = join(',', @splat);