Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
I've never used the :raw layer and there appears to be some weirdness here. I'm not sure what "read a "line" with :raw" actually means? It looks to me like it removed the <CR> just like the normal layer would. When reading binary data, I have always set binmode and read up to a requested number of bytes into a buffer without the concept of a "line ending".

Setting just the :raw layer should be equivalent to setting binmode (if not, either the documentation in PerlIO or the implementation is broken), and resulting in a completely unmodified byte stream between perl and whatever is at the other end of the handle (file, socket, pipe, ...). readline will then behave as on Unix: neither \r nor \n are translated in any way when writing, and neither <CR> nor <LF> will be translated in any way when writing (Update:) reading. This results in perl treating <LF> as \n when reading, and so all text having <CR><LF> line endings will be read as ending with \r\n, chomp with a default $/ of "\n" will chew of just the \n and leave the line read with a trailing \r (read as <CR>).

See also "newlines" in perlport, and ":raw" in PerlIO.

I never did come up with a "clean, high performance" way to handle any variation of Mac, Unix and Windows line endings easily. I just don't support old Mac and that is good enough for my users.

$text=~s/\r\n/\n/g; $text=~s/\r/\n/g; should convert CRLF (DOS) to just LF (Unix), then bare CR (Mac) to just LF, resulting in $text having \n exactly at every line ending, no matter if the input had MAC, DOS, or Unix line endings. In a file with mixed line endings (which is IMHO a broken file), this may accidentally remove a few empty lines.

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

In reply to Re^5: Dealing with files with differing line endings by afoken
in thread Dealing with files with differing line endings by dd-b

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 making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2022-05-17 10:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (65 votes). Check out past polls.

    Notices?