Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^5: different length of a line from linux and windows textfile? ("\r")

by fishmonger (Pilgrim)
on Mar 17, 2014 at 22:00 UTC ( #1078687=note: print w/ replies, xml ) Need Help??


in reply to Re^4: different length of a line from linux and windows textfile? ("\r")
in thread different length of a line from linux and windows textfile?

On Windows chomp will remove both "\n" and "\r\n".

D:\test>od -c win32.txt 0000000 l i n e o n e \r \n l i n e +t 0000020 w o 0000022 D:\test>od -c nix.txt 0000000 l i n e o n e \n l i n e t +w 0000020 o \n 0000022 D:\test>perl -pi.bak -e "chomp $_;" win32.txt D:\test>perl -pi.bak -e "chomp $_;" nix.txt D:\test>od -c win32.txt 0000000 l i n e o n e l i n e t w +o 0000020 D:\test>od -c nix.txt 0000000 l i n e o n e l i n e t w +o 0000020


Comment on Re^5: different length of a line from linux and windows textfile? ("\r")
Download Code
Re^6: different length of a line from linux and windows textfile? (still wrong)
by tye (Cardinal) on Mar 17, 2014 at 22:34 UTC

    No. You are wrong. Yes, you show code that uses chomp and results in "\r"s being dropped. Now drop the chomp and see that there are still no "\r" characters. As I already explained, the lack of "\r" has nothing to do with chomp.

    On Windows chomp will remove both "\n" and "\r\n".

    It is a lack of binmode that strips "\r". chomp only strips "\n". So you can also see this by adding binmode and showing that "\r" is not stripped despite your use of chomp (even though you are using Windows Perl).

    - tye        

      Maybe I'm not understanding your point, but according to my tests chomp sill strips out "\r" when using binmode on Windows.

      D:\test>od -c win32.txt 0000000 l i n e o n e \r \n l i n e +t 0000020 w o \r \n 0000024 D:\test>od -c nix.txt 0000000 l i n e o n e \r \n l i n e +t 0000020 w o \r \n 0000024 D:\test>perl -wpi.bak -e "BEGIN{binmode STDIN; binmode STDOUT;} chomp +$_;" win32.txt D:\test>perl -wpi.bak -e "BEGIN{binmode STDIN; binmode STDOUT;} chomp +$_;" nix.txt D:\test>od -c win32.txt 0000000 l i n e o n e l i n e t w +o 0000020 D:\test>od -c nix.txt 0000000 l i n e o n e l i n e t w +o 0000020

        "perl -p" uses <> not <STDIN> so your binmode STDIN does nothing (or at least happens too late, I'm not going to bother to swap in the exact details right now).

        > perl -e "binmode STDIN; binmode STDOUT; while(<STDIN>){ chomp $_; p +rint }" <win32.txt >win32.out > od -c win32.out 0000000 l i n e o n e \r l i n e t +w 0000020 o \r 0000022

        - tye        

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (11)
As of 2014-10-24 18:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (134 votes), past polls