since no one really answerd the second part, here is my solution to it: $a=~ s/'/\\'\\'/g
if you always want to replace every ' by \'\' if there is at least one ' in $a you need not check that any ' is in $a. Just use the re to replace. If you want to do more to $a if there is a quote in $a, you might be able toturn it around: First replace, then do what you want to do.
if ($a=~ s/'/\\'\\'/g) {
# there were quotes in $a
# do what you want to
}