Monks, I've got a weird problem with a script I'm writing to produce login batch files for Windows computers connecting to a Samba host running a recent RH Linux.

For the Windows machines to make sense of the files, I have to get the line endings right. If I print a line out with a ^M before the newline it works correctly, but not in a heredoc. The following snippet shows the behaviour (using cat -v to display control characters: this is a direct cut and paste:)

#! /usr/bin/perl -w use strict; open OUT, '>', 'ex1' or die "ex1: $!\n"; # print OUT <<'MULTI'; # makes no difference print OUT <<MULTI; foo^M bar^M MULTI print OUT "rat^M\n"; print OUT "bar^M\n"; close OUT; __PRODUCES__ foo bar rat^M bar^M

Those ^Ms really are control-Ms. I've also tried binmoding the OUT filehandle but it makes no difference. I've never run into this before. Can someone explain to me what's going on? Why aren't the control-Ms appearing in the part of the file that was printed from a heredoc? What's the workaround (other than running a unix2dos script on the resulting file afterwards)? This is perl 5.8.0. Thanks.

update: the various suggestions below do indeed fix the problem, and for that I thank my fellow monks. But I still don't know why the behaviour occurs. And nor is it related only to heredocs. An ordinary "double-quote" string has the same effect. Embed a ^M\n and it will be printed out correctly. But a trailing ^M at the end of a line in a multiline string will be silently ignored. I suppose ysth is on the right track. In this case, perl is too smart for its own good.


In reply to Can't create DOS line endings under Unix in heredocs by grinder

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.