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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |