use IO::File qw( &SEEK_SET ); my $fh = IO::File->new("+) { my $line = $_; chomp $line; $line =~ s/foo/f/g; push @output, $line; } my $output = join("\n", @output); $fh->seek( 0 , SEEK_SET ); $fh->print($output, "\n"); $fh->truncate( length($output) ); $fh->close; exit 0;