Slurp the file, edit the lines, write the file back out:

use strict; use warnings; my @edits = ( delete => ['my'], delete => ['open'], delete => ['rename'], delete => ['{'], delete => ['}'], delete => ['^\\s*$'], delete => ['\\$_'], replace => ['use.*;', '# Strictures are really important'], ); edit ('noname.txt', @edits); sub edit { my ($file, @edits) = @_; my $old = $file; # Old file my $new = "$file.tmp.$$"; #New File to create my $bak = "$file.bak"; #backup of old file my %dispatch = ( insert => \&insert_line, delete => \&delete_line, replace => \&search_replace, ); $old = $file; $new = "$file.tmp.$$"; $bak = "$file.bak"; open my $OLD, '<', $old or die "Can't open $old: $!"; my $inLines; @$inLines = <$OLD>; close ($OLD); while (@edits >= 2) { my ($edit, $params) = splice @edits, 0, 2; my $outLines = []; next unless exists $dispatch{$edit}; $dispatch{$edit}->($inLines, $outLines, @$params); $inLines = $outLines; } open my $NEW, '>', $new or die "Can't open $new: $!"; print $NEW @$inLines; close ($NEW); rename ($old, $bak); rename ($new, $old); } sub insert_line { my ($in, $out, $after, $insertline) = @_; for (@$in) { $_ .= $insertline if /$after/; push @$out, $_; } } sub search_replace { my ($in, $out, $find, $replace) = @_; for (@$in) { s/$find/$replace/g; push @$out, $_; } } sub delete_line { my ($in, $out, $delete) = @_; for (@$in) { push @$out, $_ unless /$delete/; } }

run against itself generates:

# Strictures are really important # Strictures are really important replace => ['# Strictures are really important', '# Strictures are + really important'], ); edit ('noname.txt', @edits); insert => \&insert_line, delete => \&delete_line, replace => \&search_replace, ); $old = $file; $new = "$file.tmp.$$"; $bak = "$file.bak"; @$inLines = <$OLD>; close ($OLD); $inLines = $outLines; print $NEW @$inLines; close ($NEW); s/$find/$replace/g;

DWIM is Perl's answer to Gödel

In reply to Re: Perl File Editing Subroutines, Any ideas? by GrandFather
in thread 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.