my $rx = qr/\Q$term/; while (my @row = $sth->fetchrow_array()) { s!$rx!"$term"! for @row; # ... }