This code doesn't change the file at all. So, I tried various replacements for the regex line:sub dos2unix { # Takes array containing file-paths as argument my (@files2Convert) = @_; foreach my $file (@files2Convert) { if (-e $file) { print "Converting $file...\n"; # load file contents into memory for conversion open(FILE, "$file") or die "Can't open $file: $!"; my @lines = <FILE>; close(FILE) or die "Can't close $file: $!"; # here's where the actual conversion takes place foreach my $i ( 0..$#lines ) { $lines[$i] =~ s/\015\012/\012/g; } # make backup copy of file in case this module dies halfway th +rough # the write to the new file. (We only have this file info in m +emory at that point.) rename("$file","${file}.bak") or die "Couldn't make backup copy of + $file: $!"; # write new data open(FILE,">$file") or die "Can't open $file: $!"; print FILE @lines; close(FILE) or die "Can't close $file: $!"; # remove backup if all is successful unlink "${file}.bak" or warn "Couldn't remove backup of $file: $!" +; } } }
All with the same result- no change, still CRLF. I even read up in the Camel book about the Socket module, and tried the following:$lines[$i] =~ s/\015\012/\012/g; $lines[$i] =~ s/\n/\012/g; $lines[$i] =~ s/\x0D\x0A/\012/g; $lines[$i] =~ s/\015\012/\x0A/g; $lines[$i] =~ s/\n/\x0A/g; $lines[$i] =~ s/\x0D\x0A/\x0A/g;
But the file remained CRLF.I did some experimentation, and it looks like \012 is set to "CRLF" on my system somehow because the following removes the entire line delimiter:use Socket qw(:DEFAULT :crlf); local ($/) = LF; $lines[$i] =~ s/$CRLF/$LF/g;
I copied the code over to UNIX with a few CRLF files, and it worked fine. I'm sure I'm missing something simple, has anyone else encountered this before?$lines[$i] =~ s/\012//g;
In reply to Perl Dos2Unix on Windoze? by lorenak
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |