The problem is that
\b marks a "word boundary", that is, a place where one side must be a a word character (alphanumeric or
_) and the other side isn't a word character. If the text in
$symbol begins and ends with word characters, then it does what you want. But if the text in
$symbol is, say, a comma, which
isn't a word character, then your test is ensuring that it's surrounded on both sides by word characters. So what you want is not to assert a boundary, but rather that
$symbol not be preceded by or followed by wordchars.
if ($sentence =~ /(?<!\w)$symbol(?!\w)/){
# do something
}