Hi all, I checked the FAQ for help, and the code section, but I'm still at a loss. I'm trying to write a little dos2unix module that runs on Windows 2000. I found and borrowed the version here at http://www.perlmonks.org/index.pl?node_id=215707. I only need to convert CRLF to LF, DOS to UNIX. However, no matter how I try the conversion, it seems that I can't get rid of that pesky CR. Here's what I have:
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: $!" +; } } }
This code doesn't change the file at all. So, I tried various replacements for the regex line:
$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;
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:
use Socket qw(:DEFAULT :crlf); local ($/) = LF; $lines[$i] =~ s/$CRLF/$LF/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:
$lines[$i] =~ s/\012//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?

In reply to Perl Dos2Unix on Windoze? by lorenak

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.