I do not understand your examples! If I interpret your first one as
my $valid0=q("hello"); it is invalid because it contains double quotes. If I ignore the surrounding quotes, there is nothing wrong with
my $invalid0 = q('bad);. I cannot interpret them as valid Perl code because
"bad is not. If there is any consistent way to read your examples, I cannot find it. You want a Perl solution. Please post your test cases as valid Perl strings.