Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
As of today using Strawberry Perl 5.16.0.1 (64bit), Perl doesn't automatically adapt chomp at all to the platform nor converts automatically the carriage return from files that are read.

But Perl doesn’t work like that. As ikegami explained in his post up-thread, removal of the carriage return (CR) character occurs before the string is ever fed to chomp. See the section Defaults and how to override them in PerlIO. On Windows platforms the IO layers default to unix (the lowest level layer) plus a crlf layer on top of this. This crlf is:

A layer that implements DOS/Windows like CRLF line endings. On read converts pairs of CR,LF to a single "\n" newline character. On write converts each "\n" to a CR,LF pair.

So, it is the crlf layer that converts each literal 0d0a to Perl’s logical newline "\n" (0a), and this happens as each line of the file is read in.

The input record separator, $/, defaults to "\n" (0a), Perl’s logical newline character, regardless of the platform. And chomp($string) removes from the end of $string the character(s) (if any) currently assigned to $/. Which all works out fine, because on Windows each CRLF has already been converted into a LF by the crlf IO layer before chomp comes into play.

Now, I can’t test Strawberry Perl 5.16.0.1 (64bit) as I don’t have a 64-bit OS. However, if the latest 64-bit version of Strawberry Perl does handle CRs incorrectly, it’s likely the difference is due to a change in the default IO layers. It’s unlikely to be in any way related to the implementation of the chomp function, for the reasons outlined above. I can confirm that the 32-bit versions of Strawberry Perl 5.14.2 and 5.16.0 (64int) treat carriage returns identically when reading in a text file under Vista.

Of course, the above applies only to text files. If binmode is applied to a file handle before it is read, conversion is suppressed, CRs are retained, and chomp has no effect on these CRs (unless $/ has been re-assigned).

Update: Try specifying the input layer explicitly:

open(my $fh, '<:crlf', $filename) or die ...

Athanasius <°(((><contra mundum


In reply to Re^4: chomp() is confusing by Athanasius
in thread Why chomp() is not considering carriage-return by jesuashok

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2022-06-30 18:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My most frequent journeys are powered by:









    Results (98 votes). Check out past polls.

    Notices?