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

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

by SimonPratt (Friar)
on Mar 17, 2014 at 16:48 UTC ( [id://1078628]=note: print w/replies, xml ) Need Help??


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

You are right that chomp wont pick up \r on its own, however it absolutely will pick up \r\n and correctly remove both characters, so the only time this would be an issue is if your file is corrupted, or specifically crafted to utilise \r in some way.

  • Comment on Re^3: different length of a line from linux and windows textfile? ("\r")

Replies are listed 'Best First'.
Re^4: different length of a line from linux and windows textfile? ("\r")
by choroba (Cardinal) on Mar 17, 2014 at 17:01 UTC
    chomp removes \r\n only on MSWin. On Linux, it only removes \n:
    ~$ perl -E '$x = "|\r\n";print $x;chomp $x; print $x' | xxd 0000000: 7c0d 0a7c 0d |..|.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      No, you are both wrong. chomp just removes a trailing "\n" (by default). On every platform (I'm ignoring how ancient MacOS misdefined "\r", of course).

      Running Perl on Windows will likely cause a "\r\n" in a file to end up as just "\n" in your Perl string. In that case, the fact that chomp does nothing to "\r" doesn't cause a problem. But it is not chomp that is getting rid of the "\r" for you.

      You can actually make chomp get rid of "\r\n" by setting $/ = "\r\n", but that also makes chomp not get rid of "\n" (unless it is immediately preceded by "\r"). So that's a worse idea.

      s/\s+$// is a much better idea than chomp.

      - tye        

      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

        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        

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-04-19 22:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found