Writing DOS CRLF via Unix Perl

Writing DOS CRLF via Unix Perl

am 08.11.2007 21:33:37 von chuckr

I run a script on unix Perl to write a text file. By default, when Perl
writes "\n" it writes a line ending sequence which is native to the
current OS. How do I force this particular script to always write DOS
CRLF line endings?

Thanks.

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

RE: Writing DOS CRLF via Unix Perl

am 09.11.2007 00:39:46 von David.Wagner

> -----Original Message-----
> From: C.R. [mailto:chuckr@gaw.com]=20
> Sent: Thursday, November 08, 2007 12:34
> To: beginners@perl.org
> Subject: Writing DOS CRLF via Unix Perl
>=20
> I run a script on unix Perl to write a text file. By default,=20
> when Perl=20
> writes "\n" it writes a line ending sequence which is native to the=20
> current OS. How do I force this particular script to always write DOS=20
> CRLF line endings?=20
$/ is the input rcd separator
$\ is the output rcd separator
You can set as want them to be if other than the std defaults
are desired for a particular processing.
If you have any problems or questions, please let me know.

Thanks.

Wags ;)
David R Wagner
Senior Programmer Analyst
FedEx Freight
1.408.323.4225x2224 TEL
1.408.323.4449 FAX
http://fedex.com/us=20

>=20
> Thanks.
>=20
> --=20
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>=20
>=20
>=20

************************************************************ **********
This message contains information that is confidential and proprietary to F=
edEx Freight or its affiliates. It is intended only for the recipient name=
d and for the express purpose(s) described therein. Any other use is proh=
ibited.
************************************************************ **********


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Writing DOS CRLF via Unix Perl

am 09.11.2007 05:04:12 von Tom Phoenix

On 11/8/07, C. R. wrote:

> I run a script on unix Perl to write a text file. By default, when Perl
> writes "\n" it writes a line ending sequence which is native to the
> current OS. How do I force this particular script to always write DOS
> CRLF line endings?

You can't change what "\n" means, but it's not too hard to put CRLFs
into your output. Here's the commonest way:

my $CRLF = "\x0d\x0a";
print "There was a young lady... tut, tut!$CRLF";
print "So you think that you're in for some smut?$CRLF";
print "Some five-line crescendo$CRLF";
print "Of lewd innuendo?$CRLF";
print "Well, you're wrong. This is anything but...$CRLF";
print " --Stanley J. Sharples$CRLF";

For what may or may not be extra convenience, consider using the
special variable $\ to automatically output your newline of choice
after each invocation of print.

http://perldoc.perl.org/perlvar.html

I hesitate to mention this last alternative, but you could
post-process the output instead of fixing the source. That would only
be suitable if the source code is large and complex to update -- that
is to say, source code so complex that applying s#\\n#\$CRLF#g to it
would be a step in the wrong direction.

Hope this helps!

--Tom Phoenix
Stonehenge Perl Training

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

RE: Writing DOS CRLF via Unix Perl

am 09.11.2007 10:05:53 von Jenda Krynicky

From: "Wagner, David --- Senior Programmer Analyst ---
> > -----Original Message-----
> > From: C.R. [mailto:chuckr@gaw.com]
> > Sent: Thursday, November 08, 2007 12:34
> > To: beginners@perl.org
> > Subject: Writing DOS CRLF via Unix Perl
> >
> > I run a script on unix Perl to write a text file. By default,
> > when Perl
> > writes "\n" it writes a line ending sequence which is native to the
> > current OS. How do I force this particular script to always write DOS
> > CRLF line endings?
> $/ is the input rcd separator
> $\ is the output rcd separator
> You can set as want them to be if other than the std defaults
> are desired for a particular processing.
> If you have any problems or questions, please let me know.


You'd better not. Fiddling with those GLOBAL variables can break a
lot of modules expecting them to have their default values.
You should only modify $/ in a small block and make sure you do not
call any function that might want to read anything from a file or
socket or pipe or ...
And you should not fiddle with $\ as it affects all print()
statements. Not just those going to your file. Besides it doesn't
convert \n in the middle of a printed string to CRLF ... all it does
is that it prints a CRLF (or whatever you set it to) after each
print. So this

print HANDLE "First line\nSecond line";
print HANDLE "Third line";

causes the file to contain (on unix)

First lineLFSecond lineCRLFThird lineCRLF

Again you are very likely to break things by changing $\.

It's better to specify that you want the \n -> CRLF conversion for
that particular filehandle:

open OUT, '>:crlf', 'the_file.txt';

HTH, Jenda

===== Jenda@Krynicky.cz === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Writing DOS CRLF via Unix Perl

am 09.11.2007 10:18:30 von Xavier Noria

On Nov 8, 2007, at 9:33 PM, C.R. wrote:

> I run a script on unix Perl to write a text file. By default, when
> Perl
> writes "\n" it writes a line ending sequence which is native to the
> current OS. How do I force this particular script to always write DOS
> CRLF line endings?

A good approach is to hard-code CRLF:

my $CRLF = "\015\012";

and then output that by hand the same way you would append "\n":

print "foo bar baz$CRLF";

You don't mention the runtime platform. If you are sure the runtime
platform uses Unix line-ending conventions you're done. If you need
the script to be portable or don't want to leave there that brittle
assumption you need to work in binmode.

Otherwise, since CRLF has a LF, on Windows you'd end up having triple
CRCRLF on disk, because the I/O layer translates LF -> CRLF on writing
no matter the surrounding characters.

Even if I weren't the author :-) I'd add a pointer to this article:

Understanding Newlines
http://www.onlamp.com/pub/a/onlamp/2006/08/17/understanding- newlines.html

-- fxn




--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Writing DOS CRLF via Unix Perl

am 09.11.2007 11:01:55 von ROB.DIXON

Wagner, David --- Senior Programmer Analyst --- WGO wrote:
>> -----Original Message-----
>> From: C.R. [mailto:chuckr@gaw.com]
>> Sent: Thursday, November 08, 2007 12:34
>> To: beginners@perl.org
>> Subject: Writing DOS CRLF via Unix Perl
>>
>> I run a script on unix Perl to write a text file. By default,
>> when Perl
>> writes "\n" it writes a line ending sequence which is native to the
>> current OS. How do I force this particular script to always write DOS
>> CRLF line endings?
>
> $/ is the input rcd separator
> $\ is the output rcd separator
>
> You can set as want them to be if other than the std defaults
> are desired for a particular processing.
>
> If you have any problems or questions, please let me know.

Unfortunately this doesn't solve the problem David. The value of $\
is printed after every call to print (not printf), as a sort of implicit
last parameter. By default it is undefined and nothing extra is output,
but setting it to "\n" has exactly the same effect as explicitly printing
a newline: an appropriate line terminator is output according to the
platform.

If linefeed is expressed in hex as "\x0A" it won't be interpreted as a line
terminator and will be output untranslated to the stream. This can be used
independently or in conjunction with $\.

Alternatively, setting a stream to binmode will also prevent "\n" from being
translated, but this is a less desirable solution as it implies things about
the output that are untrue.

Rob


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/