my $input = "CAT,DOG,BIRD,COW"; $input =~ /\A\w*(?:,\w*)*\z/ or die "invalid input format"; my $str = join ',', map { "'$_'" } split /,/, $input;