In unix the default line ending is a line feed LF (\n or \012 or 0xA). In DOSWin it is carriage return line feed CRLF (\r\n or \015\012 or 0xA0xD). On a Mac it is CR (\r or \015 0xD)

When you read and write text files perl will automatically convert from its internal use of \012 for the line ending to whatever the system is using. On unix this means it does nothing, but on Mac and Windows conversions are made.

Binmode tells perl not to convert line endings when reading or writing. In other words it does a raw read/write. On unix binmode has no effect as there is no conversion to make. On other systems the results are quite predictable.

Perl uses \012 as the default line ending so if you binmode an output file handle such as STDOUT, $fh, etc and then print "blah \n"you will write \012. This will not be correctly recognised as a line ending on DOSWin or Mac when trying to read this file - this is true for any program including perl programs. Unix however, will read this file fine as will perl running under unix.

When you binmode an input filehandle such as STDIN, $fh, etc you get a raw read. Thus on DOSWin if you read in a textfile under binmode you will see that the line ending is \r\n. Visually this appears a double spaced lines.

How *your* script behaves is system dependent. When you read from a file you read one line at a time as defined by the default system line ending - internally this will be represented as \n Let's assume you have a file that has \r\n line endings. On unix you will get an internal file with \r\n because no conversion is done. Reading the same file under DOSWin will get an internal file with only \n line endings.

With binmode on an output FH when you output \r\n that is what you get. With binmode off things will differ. On unix you will still get \r\n. On DOSWin you will get \r\r\n as the \n is converted to \r\n.

Unless you are writing text files across systems you do not need to worry too much. If you need binmode on the inplace edit script just binmode STDOUT.

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print


In reply to Re: Re: Re: Can anyone figure how this works? by tachyon
in thread Can anyone figure how this works? by thesundayman

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.