#!/usr/bin/perl use warnings; use strict; my $file = "txtfile"; # Read the file into $old_text: # open my $old_fh, "<$file"; my $old_text; do { local $/; $old_text = <$old_fh> }; close $old_fh; # Do the substitution # ( my $new_text = $old_text ) =~ s/\bfrog\b/toad/g; # Write the new "toad" data out, overwriting the old file # open my $new_fh, ">$file"; print $new_fh $new_text; close $new_fh;