# This program demonstrates prompting the user for input, searching, # and file manipulation. It prompts the user for an input filename, # an output filename, a search filename and a replacement string, and # replaces all occurences of the search pattern with the replacement # string while copying the input file to the output file. # #Learning Perl Appendix A, Exercise 10.2 print "Input file name: "; chomp($infilename = ); print "Output file name: "; chomp($outfilename = ); print "Search string: "; chomp($search = ); print "Replacement string: "; chomp($replace = ); open(IN,$infilename) || die "cannot open $infilename for reading: $!"; ## optional test for overwrite... die "will not overwrite $outfilename" if -e $outfilename; open(OUT,">$outfilename") || die "cannot create $outfilename: $!"; while () { # read a line from file IN into $_ s/$search/$replace/g; # change the lines print OUT $_; # print that line to file OUT } close(IN); close(OUT);