Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Native newline encoding

by Anonymous Monk
on May 22, 2012 at 21:30 UTC ( #971882=note: print w/ replies, xml ) Need Help??


in reply to Native newline encoding

You probably really shouldn't do any detecting :)

You should be liberal in what you accept  s/[\r\n]+$//; and be strict in what you output  binmode  $fh, '...:crlf';

See also File::LocalizeNewlines and Encode::Newlines, PerlIO::eol and Devel::CheckOS


Comment on Re: Native newline encoding
Select or Download Code
Re^2: Native newline encoding (more liberal)
by tye (Cardinal) on May 23, 2012 at 02:52 UTC
    s/[\r\n]+$//;

    I much prefer s/\s*$//; because one should never write new code that causes trailing whitespace to be significant.

    be strict in what you output  binmode  $fh, '...:crlf';

    That seems like something that is quite unlikely to be what one should do. That might make sense when trying to use a Unix system to write a text file that will be used by some MS Windows program(s).

    For the most common case, you should replace that 'binmode' code with this code:

    - tye        

      That seems like something that is quite unlikely to be what one should do. That might make sense when trying to use a Unix system to write a text file that will be used by some MS Windows program(s).

      You mean like this exact situation? user wants notepad.exe to open .ini file and for it to work?

      For the most common case, you should replace that 'binmode' code with this code:

      What code?

Re^2: Native newline encoding
by salva (Monsignor) on May 23, 2012 at 08:53 UTC
    Interestingly, Encode::Newlines does it as follows:
    use constant Native => ( ($^O =~ /^(?:MSWin|cygwin|dos|os2)/) ? CRLF : ($^O =~ /^MacOS/) ? CR : LF );

    I guess that this, which some special handling for VMS where the line ending is set by file and EDBCDI systems where line endings are the lesser problem, should cover 99% of the cases.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2014-09-19 02:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (129 votes), past polls