Highlights
All Snippets
Top 100 Snippets
Librarians
gbCodeLib

By Language
VB6
JavaScript
Perl
HTML
SQL
Java
DOS

GBIC >> Source Code >> Perl >> Snippet

selectively save part of file


$old = $file ;
$new = "$file.tmp.$$" ;
$bak = "$file.orig" ;

open (OLD, "< $old" )   or die "can't open $old: $!" ;
open (NEW, "> $new" )   or die "can't open $new: $!" ;

# Correct typos, preserving case
while ( < OLD > ) {
    s /\b(p)earl\b/${1}erl/i;
   ( print NEW $_ )   or die "can't write to $new: $!" ;
}

close (OLD)    or die "can't close $old: $!" ;
close (NEW)     or die "can't close $new: $!" ;
rename ( $old , $bak )   or die "can't rename $old to $bak: $!" ;
rename ( $new , $old )   or die "can't rename $new to $old: $!" ;