Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
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.

Replies are listed 'Best First'.
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?
[perldigious]: Also coincidentally, I'm currently in the middle of a winter storm that has dropped 1 foot of snow and is expected to drop another. I might have to adapt this for customers/vendors who come visit and put it at the beggining of every presentation I give.
[Discipulus]: the photo was removed... Cyrano de Berjerac was not of the same idea..
[stevieb]: perldigious: video near our hq in Montreal. Here in Calgary, it's -28c... we've only got 3" of snow so far, but there are several feet in the mountains already
[perldigious]: Snowshoeing time stevieb? I need to buy a pair so I can walk my German Shepherd properly through the country.
[stevieb]: yeah, I snowshoe a fair amount in the mountains. I also use spikes similar to these very often as well

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (11)
As of 2016-12-06 16:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (112 votes). Check out past polls.