Unix-format text from STDOUT under Windows (AcitvePerl 5.8.x)?

J

Jim Monty

Using ActivePerl 5.8.x for Windows, how do I make STDOUT output
Unix-format text instead of DOS-format text? In other words, I want to
force the output text to have LF-terminated lines, not CRLF-terminated
lines. The input text is DOS-format.

Jim Monty
 
S

Shawn Corey

Jim said:
Using ActivePerl 5.8.x for Windows, how do I make STDOUT output
Unix-format text instead of DOS-format text? In other words, I want to
force the output text to have LF-terminated lines, not CRLF-terminated
lines. The input text is DOS-format.

Jim Monty

You could try 'binmode'. I'm not running MS so I can't test it.

perldoc -f binmode

--- Shawn
 
P

Paul Lalli

Jim Monty said:
Using ActivePerl 5.8.x for Windows, how do I make STDOUT output
Unix-format text instead of DOS-format text? In other words, I want to
force the output text to have LF-terminated lines, not CRLF-terminated
lines. The input text is DOS-format.

Perhaps this is naive, but why not just output what you want to output:

print "This is a line ending in LF\012";

In fact, I *think* that's what the documentation (perldoc perlop - Quote
and Quote-like operators) suggests as well.

Paul Lalli
 
J

Jim Monty

Shawn said:
You could try 'binmode'. I'm not running MS so I can't test it.

perldoc -f binmode

Thanks. binmode() works. It's unclear from perldoc -f binmode and
perldoc PerlIO exactly which of these is most appropriate, but they all
seem to work:

binmode STDOUT;
binmode STDOUT, ":raw";
binmode STDOUT, ":unix";

Alas, I asked the wrong question in the first place: the right answer
still doesn't solve my real problem. I want to change the behavior of
XML::Writer, but it seems there's no way to do it. It mucks with STDOUT
using IO::Handle in a way that a user of the class cannot alter.
Oh well!

Jim Monty
 
M

Michael Carman

Jim said:
Using ActivePerl 5.8.x for Windows, how do I make STDOUT output
Unix-format text instead of DOS-format text?

I too once looked into ways to have Perl generate text files in
something other than the native platform format. IIRC, binmode() worked
for some situations but wasn't flexible enough for the general case. I
finally decided that it was better to either

a) post-process the files, or
b) use an FTP client to transfer them in ASCII mode

-mjc
 
B

Bart Lateur

You could try 'binmode'. I'm not running MS so I can't test it.

perldoc -f binmode

That should work, yes. Note that "\n" on Windows is a LF, chr(10).
Reading from a text filehandle (no binmode) converts CRLF to "\n" on
input, and printinf to a text filehandle will convert "\n" into CRLF.
binmode prevents that conversion, leaving you with a "Unix text file".
 
J

Jim Monty

Michael said:
I too once looked into ways to have Perl generate text files in
something other than the native platform format. IIRC, binmode() worked
for some situations but wasn't flexible enough for the general case. I
finally decided that it was better to either

a) post-process the files, or
b) use an FTP client to transfer them in ASCII mode

In fact, that's what I've been doing in this case: post-processing the
file.

C:\>perl ScriptThatUsesXMLWriter.pl Input.csv | tr -d "\r" >Output.xml

But now I'd like to be able to hand the Perl script to someone who will
have ActivePerl, but not necessarily tr (Cygwin or MKS Toolkit). Using
binmode() would work splendidly if I were controlling STDOUT, but I'm
not: XML::Writer is. (I explained this in a second follow-up post.)
Jim Monty
 
S

Shawn Corey

Jim said:
In fact, that's what I've been doing in this case: post-processing the
file.

C:\>perl ScriptThatUsesXMLWriter.pl Input.csv | tr -d "\r" >Output.xml
#/usr/bin/perl

use strict;
use warnings;

close STDOUT;
open STDOUT, "| tr -d \"\\r\"" or die $!;

....

close STDOUT or die $!; # flush last buffer

__END__

--- Shawn
 
M

Michael Carman

Jim said:
In fact, that's what I've been doing in this case: post-processing
the file.

C:\>perl ScriptThatUsesXMLWriter.pl Input.csv | tr -d "\r" Output.xml

But now I'd like to be able to hand the Perl script to someone who
will have ActivePerl, but not necessarily tr (Cygwin or MKS Toolkit).

So write a trivial Perl utility to do it and give it to the user along
with your script:

#!/usr/bin/perl -w
binmode(STDIN);
binmode(STDOUT);
while (<STDIN>) {
s/\015\012/\012/;
print;
}

[apologies if the formatting is poor, google groups seems to strip what
it deems to be extraneous whitespace.]

Then you can run

c:\>perl xmlscript.pl input.csv | perl tr.pl > output.xml

or, after using pl2bat on the translator:

c:\>perl xmlscript.pl input.csv | tr.bat > output.xml

It's not ideal (it would be nicer if you could hide that detail inside
your main script) but it's functional.

-mjc
 

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,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top