I wrote this snippet of code to allow me to 'easily' change files via program rather than hand editing. I made 3 subroutines that take input, and then perform edits on the file.

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);
"Two Wheels good, Four wheels bad."

In reply to Perl File Editing Subroutines, Any ideas? by symgryph

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.