I first thought, this is easy, but... I have existing code which produces on STDOUT and STDERR a bunch of lines in a pretty usual way, i.e.

print "something"; .... print " here the line ends\n"; .... print STDERR "and here something on stderr\n";
Of course the \n is actually output as line feed if the application runs on Unix/Linux/Cygwin, and to carriage return - line feed when run on Windoze. So far, nothing unusual.

Now I would like to add options to the app, where the user could say "no matter what platform we are running, always use Unix (or Windows) line endings". My first approach went like this:

Getoptions(....); # Set output record separator $\="\r\n" if $dos_option; $\="\n" if $unix_option; ....
But this fails, because $\ is - as I found out after reading the documentation more carefully - appended after every print statement. Hence if S\ is set to "dos style", a
print "abc\n"
would end up in "abc\n\r\n" being output.

Is there an easy way to solve my problem? One solution would be to define a variable $lineending appropriately, set binmode on STDOUT and STDERR and, in the application, replace ever \n by $lineending, but this is a bit ugly. Any better idea?
-- 
Ronald Fischer <ynnor@mm.st>

In reply to Controlling whether output goes in DOS or Unix file format by rovf

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.