drodinthe559 has asked for the wisdom of the Perl Monks concerning the following question:

Can anyone tell me what's the best way to format a streaming text file from a mainfram in Perl? Right now I have the a dts package in sql server that imports and export it out with the CR/LF at the end of the line. Any suggestions will be greatly appreciated. Dave

Replies are listed 'Best First'.
Re: Format Streaming Text file with CR/LF
by moritz (Cardinal) on Jul 22, 2008 at 18:44 UTC
    I don't know if you want to add or remove the CR/LF, but in either case the <c>:crlf IO-Layer might help:
    binmode HANDLE, ':crlf';

    If not, try asking a more specific question.

Re: Format Streaming Text file with CR/LF
by ikegami (Patriarch) on Jul 22, 2008 at 18:56 UTC
    If I understand correctly, you are trying to read in a file where the record (line) seperator is CRLF. So why don't you set the input record seperator to CRLF?
    # Any system binmode $fh; local $/ = "\x0D\x0A"; while (<$fh>) { chomp; ... }

    Note that Perl on Windows system covertly converts CRLF to LF on input, so you don't have to do anything.

    # Windows only while (<$fh>) { chomp; ... }
      I'm trying to add the CR/LF in a line feed text file. The text file when I get it is on long line with no CR/LF in it.I want to create a new line at every 90th character.

        In that case, I pretty much answered this question yesterday.

        From a file handle:

        binmode $fh_in; binmode $fh_out; local $/ = \90; while (<$fh_in>) { print $fh_out "$_\x0D\x0A"; }

        From a string:

        binmode $fh_out; while (length($str)) { my $rec = substr($str, 0, 90, ''); print $fh_out "$rec\x0D\x0A"; }

        If you want CRLF when run on Windows and LF when run on other OSes, get rid of the binmode and replace \x0D\x0A with \n.

Re: Format Streaming Text file with CR/LF
by pc88mxer (Vicar) on Jul 22, 2008 at 18:46 UTC
    The best way depends on what application is going to use the outputted data. On Windows the standard is to use CR-LF at the end of lines, but the convention for Unix systems is just LF.
      It is going to be used on a Windows system. I'm not sure what the best approach to adding the CR/LF at every 90 position.