use strict; use warnings; sub subsitute { (my $filename, my $oldword, my $newword) = @_[0..2]; local $/; if(open(my $fh, "<", $filename)){ my $contents = <$fh>; $contents =~ s/\b$oldword\b/$newword/ig; close($fh); if(open($fh, ">", $filename){ print $fh $contents; close($fh); return 1; # for success } else{ warn "Could not open $filename for writing because $!; skipping it\n"; return undef; } } else { warn "Could not open $filename because $!; it will be skipped\n"; return undef; } }