$intext =~ s/$orgtext/$newtext/ms; # the ms is for coping correctly with newlines (that can easily appear in a binary). ... # replaces ALL occurrences of orgtext with newtext and places the number of occurences in $count