rovf has asked for the wisdom of the Perl Monks concerning the following question:

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>

Replies are listed 'Best First'.
Re: Controlling whether output goes in DOS or Unix file format
by Anonymous Monk on Oct 15, 2008 at 09:29 UTC

      Thanks a lot. The LAYER section of binmode looks promising ;-)

      -- 
      Ronald Fischer <ynnor@mm.st>