Here are the problems:
1. the code calls the subroutine once for each set of changes, resulting in LOTS of files being written to if I want to make multiple changes to a file, for example, replace foo with bar, and then delete the line containing foobaz.
2. As a result of multiple calls, the 'backup' files really aren't that useful, since it overwrites the original.
Any way to make this routine more generic, and able to handle multiple inserts, and deletes with only 1 call of the subroutine?
# This is more of a code snippet to include in programs. # It provides for insert, delete, and change functionality to lines of + text # within given files. Each of the subroutines takes a set of paramate +rs to invoke it. # Text below documents the usage for each of the subroutines # # insert_line uses three paramaters, the first of which is the filenam +e to insert a line into, # the second which is the line after which you want the input inserted + into, and the third which # is the text you want inserted into the file. # # search_replace uses three parameters, the first being the filename t +o be worked on, the second # being the text to search for, and the third being the text to replac +e the searched text with. # # delete_line uses two paramaters, the name of the file to delete the +text from, and the name of # the text to strike from the file. Below is invocation of delete_line +s function example # # my $filename="file"; # the filename # my $delete="Hello"; #Things to search for to insert after # my $replace="Goodbye!"; #Thing to insert. # delete_line($filename,$delete); # # to enable searching for perl regular expressions, here is an invocat +ion example: # my $search=qr{perl regular expressions}; e.g. \d+ #Uncomment the code below to call the function # # #This subroutine takes 3 paramaters, 1. the file to edit, 2. the thing + to search for, 3. text # to be inserted after the found item. It backs up the old file, crea +tes a new file, and then #the new file moviing the old file to .bak. ## sub insert_line { my $file=$_[0]; # Name of file to edit my $old = $file; # Old file my $new = "$file.tmp.$$"; #New File to create my $bak = "$file.bak"; #backup of old file my $after = $_[1]; #the thing to insert the line after (text) my $insertline = $_[2]; #the thing to insert itself $old = $file; $new = "$file.tmp.$$"; $bak = "$file.bak"; open (OLD, "< $old") or die "Can't open $old: $!"; open (NEW, "> $new") or die "Can't open $new: $!"; while (<OLD>) { if (/$after/) { $_ .= $insertline; } (print NEW $_); } close (OLD); close (NEW); rename ($old, $bak); rename ($new, $old); } # #this subroutine changes one thing to another in a file. #It takes 3 paramaters, The filename, the thing to find, and the thing + to replace. sub search_replace { my $file= $_[0]; my $old = $file; my $new = "$file.tmp.$$"; my $bak = "$file.bak"; my $find = $_[1]; my $replace = $_[2]; open (OLD, "< $old") or die "Can't open $old: $!"; open (NEW, "> $new") or die "Can't open $new: $!"; while (<OLD>) { s/$find/$replace/g; (print NEW $_); } close (OLD); close (NEW); rename ($old, $bak); rename ($new, $old); } # #This subroutine deletes a line in a file. It takes two arguments, th +e filename to operate on, #and line containing the text to be deleted. # sub delete_line { my $file= $_[0]; my $old = $file; my $new = "$file.tmp.$$"; my $bak = "$file.bak"; my $delete = $_[1]; open (OLD, "< $old") or die "Can't open $old: $!"; open (NEW, "> $new") or die "Can't open $new: $!"; while (<OLD>) { if (/$delete/) { $_= ""; } (print NEW $_); } close (OLD); close (NEW); rename ($old, $bak); rename ($new, $old);
In reply to Perl File Editing Subroutines, Any ideas? by symgryph
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |