Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re^3: \r\n at end of line (preserve \r?)

by tye (Sage)
on Apr 19, 2011 at 17:25 UTC ( [id://900177]=note: print w/replies, xml ) Need Help??


in reply to Re^2: \r\n at end of line (\s)
in thread \r\n at end of line

It is my experience that most text-processing tools don't preserve newline style (file-copying tools would be a different story). I don't expect it from other tools and I generally don't write tools that do such. If I'm reading newline-terminated lines, then I'm likely processing text, not copying data.

I expect tools to write new text files using the default newline style of the tool's platform. That is certainly what will happen with any tool written in C under Windows (unless the developer goes out of their way to change default behavior).

A simple "perl -pe1" using native Windows perl will include "\r"s in the output even if the input lines are terminated with just "\n"s (note that I didn't even have to give perl the "-l" option).

If you want to preserve newline style, then you want to at least binmode your input and output. Then you can decide whether you want to try to preserve inconsistent or non-standard newline style. I'm not a fan of either so I'd probably just support two output choices, either always "\n" or always "\r\n".

binmode STDIN; binmode STDOUT; my $newline; while( <STDIN> ) { $newline ||= /\r/ ? "\r\n" : "\n"; s/\s+$//; ... print ..., $newline; }

But at this point I don't see ever wanting to do that. :)

- tye        

Replies are listed 'Best First'.
Re^4: \r\n at end of line (preserve \r?)
by flexvault (Monsignor) on Apr 21, 2011 at 13:24 UTC

    tye

    I expect tools to write new text files using the default newline style of the tool's platform

    Just to update your thoughts on your comment and the why of my question.

    We have an environment where a *nix share server has stored templates that are used by mainframes, windows machines, macs, linux, etc. We have our own editor (originally written in C, but now re-written in perl) that is used to edit these files. Maintaining the integrity of the line endings is critical. I need to benchmark and verify all line-endings work correctly, but your solution is clean.

    Thank you

    "Well done is better than well said." - Benjamin Franklin

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (8)
As of 2024-04-19 07:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found