Trouble with printing newlines to a file

S

shaneal

I want to be able to print just an "LF" to a file.

First, to clear up any ambiguity, I'm going to call the ASCII
character with a hex code of 0A and a decimal code of 10 an "LF". I'm
also going to call the ASCII character with a hex code of 0D and a
decimal code of 15 a "CR".

As a simple test case:

open TEST, ">", "test.data";
select TEST;
print "A" . chr(hex("0A")) . "B" . "\n" . "C" . "\r" . "D";
close TEST;

Logically, when I look at the test.data file with a hex editor I'd
expect just a "LF" between the A and B, but instead there are is a
"CR" followed by an "LF."

Here is what a hex editor shows is in "test.data" (first line is hex,
second line are chars):

41 0D 0A 42 0D 0A 43 0D 44
A CR LF B CR LF C CR D

I was expecting only a "LF" (i.e., 0A) between the "A" (41) and
"B" (42) ...

If it helps this is Perl 5.8.8 (ActiveState Perl Build 820) on Windows
XP.

Thanks for any advice you may be able to give me.
 
P

Peter Wyzl

shaneal said:
I want to be able to print just an "LF" to a file.

First, to clear up any ambiguity, I'm going to call the ASCII
character with a hex code of 0A and a decimal code of 10 an "LF". I'm
also going to call the ASCII character with a hex code of 0D and a
decimal code of 15 a "CR".

As a simple test case:

open TEST, ">", "test.data";
select TEST;
print "A" . chr(hex("0A")) . "B" . "\n" . "C" . "\r" . "D";
close TEST;

Logically, when I look at the test.data file with a hex editor I'd
expect just a "LF" between the A and B, but instead there are is a
"CR" followed by an "LF."

Here is what a hex editor shows is in "test.data" (first line is hex,
second line are chars):

41 0D 0A 42 0D 0A 43 0D 44
A CR LF B CR LF C CR D

I was expecting only a "LF" (i.e., 0A) between the "A" (41) and
"B" (42) ...

If it helps this is Perl 5.8.8 (ActiveState Perl Build 820) on Windows
XP.

Given that it's Windows, you may need to binmode the file so the OS doesn't
make wrong assumptions about your file.

open TEST, ">", "test.data";
select TEST;
binmode TEST;

perldoc binmode

P
Of course I could be on the wrong track entirely....
 
S

shaneal

thanks you both very much.

i wasn't familiar with binmode. After reading some docs, it worked
flawlessly
 
T

Tad McClellan

bin mode is
[snip]

a neutral eunich term!


binmode is not a eunich's term, it is a Perl term.

It is the same term when used on other "operating systems"
too (such as the OP's).
 
D

Dr.Ruud

A. Sinan Unur schreef:
Also, if one wants specific characters, it is better to specify them
by their codes rather than using \n whose meaning depends on what the
platform thinks a line ending ought to be.


Using either "\n" of chr(10) or "\x0A" or "\x{0A}" or "\012" or "\cJ"
(etc.) seems to make no difference (on Windows, have not tested OS-X).

So just don't forget to call binmode, if the platform's line ending is
not what you want:


C:\>perl -we "print chr(10)" > test.out
C:\>debug test.out
-d
14C6:0100 0D 0A 00 00 00 00 00 00-[...]
-r
AX=0000 BX=0000 CX=0002 DX=0000[...]



C:\>perl -we "binmode STDOUT; print chr(10)" > test.out
C:\>debug test.out
-d
14C6:0100 0A 0A 00 00 00 00 00 00-[...]
-r
AX=0000 BX=0000 CX=0001 DX=0000[...]


(the value in CX is the size of the file)
 
P

Peter J. Holzer

A. Sinan Unur schreef:


Using either "\n" of chr(10) or "\x0A" or "\x{0A}" or "\012" or "\cJ"
(etc.) seems to make no difference (on Windows, have not tested OS-X).

Yes, "\n" is the same as "\x0A" on Windows. I think it's the same on
MacOS X, but it was "\x0D" on MacOS <= 9.

I don't know what EBCDIC-based versions of perl use: NL ("\x15") is
probably more natural than LF ("\x25") there.

hp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top