Replacing a pattern in file without reading the whole file- Perl -


i want replace pattern 'good' 'bad' in file. doing is:

#!/perl/bin/perl  $filename= "abc.txt";  open $fh, $filename; $text = { local( $/ ); <$fh> }; close $fh; $text =~s/good/bad/g; 

is there way can without reading whole file??

edit: suppose know there's 1 'good' in file.

p.s.,hi new here. hope doing correctly.

you use tie::file:

#!/usr/bin/perl use strict; use tie::file; $filename = ...; tie @lines, 'tie::file', $filename or die; (@lines) {     s/good/bad/g , last; } 

too make sure not whole file slurped in want read lines one-by-one, e.g. using:

#!/usr/bin/perl use strict; use tie::file; $filename = ...; tie @lines, 'tie::file', $filename or die; for(my $i=0; ; $i++) {     last if !defined $lines[$i]; # eof     last if $lines[$i] =~ s/good/bad/g; } 

Comments