Hi wise perl monks, I have a question:
zentara in a prior post had supplied some code (below) which recursively replaced text in files. You called the program for example:
perl zsr.pl 'gaggle' 'geese' txt
and it works just fine replacing gaggle with geese in .txt files. However if I try to do a dos to unix file conversion
perl zsr.pl '\r\n' '\n' txt
it finds the windows "carriage return line feed" just fine but replaces that with the text \n instead of a "line feed".
if I replace the line:
$_ =~ s/$search/$replace/g;
with
$_ =~ s/\r\n/\n/g;
it does the dos to unix conversion ok
why ?
Thanks for your time.
#!/usr/bin/perl
# by zentara
# Recursively searches down thru directories replacing patterns in fil
+es.
# usage zsr 'search' 'replace' 'ext'(optional with no .)
# use '' for null string in $replace
# ex: zsr 'type1' 'series A'
use warnings;
use strict;
use File::Find;
my ($search,$replace,$ext) = @ARGV;
if (defined $ext) {$ext = ".$ext"} else {$ext = '.*'};
die "Usage : zsr 'search' 'replace' 'extension' (extension optional)\n
+" if ($search eq "");
find (\&wanted, ".");
sub wanted {
my $open = $_;
my $tempfile = 0;
if (!($open =~ /$ext$/i) or (-d||-B||-l)) {return}
print $open,"\n";
my $mode = (stat $open)[2];
#print $mode,"\n";
#printf "Permissions are %04o\n", $mode & 07777;
open (TEMP,">> $tempfile");
open (FH, "< $open") or die "Can't open $open: $!\n";
while (<FH>) {
$_ =~ s/$search/$replace/g;
print TEMP $_;
}
close FH;
close TEMP;
rename ($tempfile, $open) or die "Can't rename $open: $!\n";
chmod ($mode,$open) or die "Can't restore permissions to $open, possib
+ly wrong owner: $!\n";
}