4 # file-replace [options] [--] filename[s]|dir[s]
5 # --in-pattern="search for"
6 # --out-pattern="replace with"
7 # --in-pattern-file="file"
8 # --out-pattern-file="file"
28 if(!$argflag && $arg=~/--([^=]*)=(.*)/){
32 if($key eq "in-pattern"){
36 if($key eq "out-pattern"){
40 if($key eq "in-pattern-file"){
41 die "Could not open file $val: $!" unless open(F
,"$val");
48 if($key eq "out-pattern-file"){
49 die "Could not open file $val: $!" unless open(F
,"$val");
57 print "Unknown option --$key\n";
62 if(!$argflag && $arg=~/--(.*)/){
63 if($key eq "no-recurse"){
67 if($key eq "no-directories"){
71 print "Unknown option --$key\n";
75 push @infiles, ($arg);
78 &recursive_doit
($pwd,@infiles);
81 my($pwd,@globlist)=@_;
85 # seperate files from directories
86 foreach $file (@globlist){
95 print "$pwd/$file is not a plain file or directory.\n";
98 # Are we called on a directory? recurse?
100 # fork into each dir with all but the original path ar
101 foreach $dir (@dirs){
104 wait; # don't hose the box ;-)
106 die "Could not chdir to $pwd/$dir: $!\n" unless chdir $dir;
108 # open and read the dir
109 die "Could not read directory $pwd: $!\n" unless
112 @globlist=grep { /^[^\.]/ && !(-l
"$_") } readdir(D
);
116 recursive_doit
($pwd,@globlist);
122 foreach $file (@files){
130 if($body=~s{$search}{$replace}g){
132 print "Performed substitution on $pwd/$file\n";
134 # replace with modified file
135 my$tempfile="file-replace-tmp_$$";
136 die $! unless open(F
,">$tempfile");
139 die "Unable to replace modified file $file: $!\n" unless
140 rename($tempfile,$file);
145 print "Could not open $pwd/$file: $!\n";