in reply to word & non-word boundries

Why don't you put the \b / \B in the patterns spec?

@patterns = qw/\B<string>\B \bline\b \bsecondline\b/; ... if ($line =~ /$patterns[$i]/i){ $line =~ s/$patterns[$i]/$replace[$i]/i;