[comp.lang.perl] How to print LF

rahardj@ccu.umanitoba.ca (Budi Rahardjo) (01/28/91)

I have problem printing (outputing) linefeed (LF).
It's always converted to LF/CR. Help.

-- budi

roy%cybrspc@cs.umn.edu (Roy M. Silvernail) (01/28/91)

rahardj@ccu.umanitoba.ca (Budi Rahardjo) writes:

> I have problem printing (outputing) linefeed (LF).
> It's always converted to LF/CR. Help.

Experiment time... this program:

open(OUTFILE,">tt.dat");
print OUTFILE "before\n";
close(OUTFILE);
open(OUT2,">>tt.dat");
binmode(OUT2);
print OUT2 "after\n";
close(OUT2);

produces a file consisting of

before<CR/LF>
after<LF>

But the intuitive solution (and the one I tried first) doesn't give the
expected result...

open(OUTFILE,">tt.dat");
print OUTFILE "before\n";
binmode(OUTFILE);
print OUTFILE "after\n";
close(OUTFILE);

gives

before<LF>after<LF>

The manual doesn't seem specific on changing modes within a given
open(), but it appears that the mode for a given file handle is
determined at compile time and applied throughout the program. Thus, a
second open is necessary to change modes like my first program above.

The solution, Budi, is to binmode() the filehandle to allow outputting the
required <LF>, and do explicit close/re-open manouvers to toggle between
modes. It also appears that you should use different handle names.

The manual _does_ state, in the open() section, that filehandles may be
duped, but also states that the dup should be of the same mode as the
original, so it appears you cannot binmode() a duped handle unless the
original is binary mode.

Aren't you Unixers glad you don't have to diddle with this stuff? ;-)
--
Roy M. Silvernail --  roy%cybrspc@cs.umn.edu - OR-  cybrspc!roy@cs.umn.edu
Department of redundancy department, or "Take the long way home...":
main(){system("perl -e '$x = 1/50; print \"Still just my \\$$x!\n\"'");}
               [new year, new .sig, same ol' cyberspace]