Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

How to control UNIX vs DOS line feeds in Perl

( #36555=categorized question: print w/ replies, xml ) Need Help??
Contributed by thoglette on Oct 13, 2000 at 08:19 UTC
Q&A  > input and output


Description:

We run on mixed platforms & have configured our editors and config mgt. tools to use Unix style \n. However Perl under Win32 wants to use DOS style lf. Neither perldoc perlvar nor perl -V seem to offer any clues to how the meaning of \n can be controlled. Is there a Perl-time switch? Is there a compile time switch (presuming I rebuild from scratch) Does it depend on the compiler itself? Can anyone enlighten me? Thog.

Answer: How to control UNIX vs DOS line feeds in Perl
contributed by Jonathan

This is more of an OS issue more than Perl. If you want the same routine to run on both then I'd suggest using the $^O variable and toggling behaviour on the value.
I use Samba a lot and here's what they say about the issue in one of their Faq's

CRLF-LF Conversions We get many requests for CRLF/LF format conversion handling by samba. The problem is that there is no clean way to determine which files should / could be converted and which MUST not be. Since Unix and DOS/Windows uses alike will use .txt to represent a file containing ASCII text we can not reliably use the file extension. The same applies to the .doc extension. Samba operates around the premise that we should leave all files uncha +nged. By not implmenting CRLF/LF conversions we can not be guilty of damagin +g anyone's files. When someone comes along with a sound implementation that guarrantees +file integrity we will jump at the opportunity to implement this feature. U +ntil such time there is no prospect for action on this topic.


Answer: How to control UNIX vs DOS line feeds in Perl
contributed by Fastolfe

I'm not quite sure what behavior you're trying to get out of this. ASCII (text) files are, by their very nature, different than binary files not only in that they tend to contain only readable data, but this data is broken up into multiple lines. Each OS handles newlines differently, as I'm sure you know. Tools written for one OS should (for major compatibility reasons) adapt to that OS's mechanism for interpreting newlines (as Perl does with \n). If you are using tools under Windows that work with text files yet do not honor Windows' newline convention, I would consider that a bug with the tools.

Along the same line, if you FTP files from one OS to another, you do so using the ASCII protocol, which correctly adapts your text file's newlines to match the conventions of the other OS, so that tools on the new OS can correctly read the lines of your file. Again, if the new OS incorrectly uses conventions from your source OS instead of the newline conventions of the OS they're running on, that is anomalous behavior that should be corrected.

If you truly need Perl to treat a file as anything but a simple text file (allowing you to set your own newline conventions), you should use binmode and treat the file as a binary file:

binmode(FH); local $/ = "\cM\cJ"; # CRLF print FH "A line of pseudo-ASCII text$/"; while (<FH>) { # This might actually work, given that it honors $/ }
If you do something like this, avoid \n, which literally means "newline", which will behave differently under different operating systems, counter to what you seem to want.

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (4)
    As of 2014-09-16 01:33 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite cookbook is:










      Results (155 votes), past polls