Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

How the perl converts LF into CRLF

by perladdict (Chaplain)
on Jun 23, 2006 at 16:30 UTC ( #557248=perlquestion: print w/ replies, xml ) Need Help??
perladdict has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I have a file on unix os,as i know that in unix every line is padded with LF i,e,default.now i am sending the content of this file to some remote server which run on win os by using perl.In that scenario by which mechanism perl will converts LF to CRLF. Waiting for your saggation.

Comment on How the perl converts LF into CRLF
Re: How the perl converts LF into CRLF
by sh1tn (Priest) on Jun 23, 2006 at 16:34 UTC
    UNIX has '\n' end of line;
    Windows family has '\r\n' instead;
    Mac OS has '\r';
    In perl you can convert (simulate dos2unix and unix2dos) like this:
    # unix2dos: # perl -i.org -pe 's/\n/\r\n/' filename # dos2unix: # perl -i.org -pe 's/\r\n/\n/' filename


      Those won't work on a Windows box because binmode wasn't used.
        You are right. Below is the dos2unix equivalent works on Windows
        #!/usr/bin/perl -w use strict; use File::Temp qw(tempfile); use File::Copy; dos_to_unix($ARGV[1]); sub dos_to_unix { my($file) = @_; my($dst, $src); my (undef, $tmpfile) = tempfile(); copy($file, $tmpfile) or die "Can't copy $file to $tmpfile due to: $!\n"; open($dst, ">$file") or die "Can't open $file for write due to: $!\n"; open($src, "<$tmpfile") or die "Can't open $tmpfile for read due to: $!\n"; binmode($dst); while(<$src>) { chomp; print $dst "$_\n"; } close($dst); close($src); }
Re: How the perl converts LF into CRLF
by derby (Abbot) on Jun 23, 2006 at 16:34 UTC

    How are you sending it? If you use Net::FTP in ascii mode, the translastion will be done auto-magically.

    -derby
      Hi Derby, thanks for suggetion,i am using Net::cmd in my script,in which i included senddata(DATA) method.

        Hmmm ... never used Net::Cmd directly. For Net::FTP the translation is not done in the client but on the server side. What type of server are you connecting to? FTP? Home grown?

        -derby

        Update: That is for all ftp ... not just Net::FTP, the translation is done in the server.

Re: How the perl converts LF into CRLF
by Joost (Canon) on Jun 23, 2006 at 16:36 UTC
Re: How the perl converts LF into CRLF
by xdg (Monsignor) on Jun 23, 2006 at 16:43 UTC

    If you don't want to do it manually, but want to understand how Perl does it, read perlport and perlio.

    One simple option is to call binmode on the filehandle on the win32 machine to disable automatic CR/LF translation and then everything will stay with LF for line endings. (Note this will cause issues for UTF-8, so if you're worried about that, you will need to read about the various IO layers described in perlio.)

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Re: How the perl converts LF into CRLF
by jdtoronto (Prior) on Jun 23, 2006 at 19:54 UTC
    Did you read the POD for the Net::cmd module? If you had you would find this:
    datasend ( DATA ) Send data to the remote server, converting LF to CRLF. Any line st +arting with a '.' will be prefixed with another '.'. DATA may be an a +rray or a reference to an array.
    jdtoronto

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://557248]
Approved by Joost
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (10)
As of 2014-10-24 09:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (131 votes), past polls