http://www.perlmonks.org?node_id=557818

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

Hello monks. I am running my code on a linux box and making a report for a windows machine. I am trying to get each line to have \r\n at the end of it. I've read perlform documentation and can't find anything on this. Is this possible? Thanks for your help. Regards,

Erik

Here is what I tried but it did not work.
#!/usr/bin/perl -w use strict; my $name = 'name'; my $home = "town\r\n"; binmode STDOUT; format STDOUT = @<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<< \r\n $name, $home . write STDOUT;

Replies are listed 'Best First'.
Re: formats output \r\n question
by shmem (Chancellor) on Jun 27, 2006 at 15:30 UTC
    Set the special variable $\ to "\r\n". See The parable of the falling droplet.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      I tried this and it didn't seem to work. I also tried comenting the binmode line out as well. thanks
      #!/usr/bin/perl -w use strict; my $name = 'name'; my $home = "town\r\n"; { local $\= "\r\n"; binmode STDOUT; format STDOUT = @<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<< \r\n $name, $home . write STDOUT; }
        Darn. Well, TIMTOWTDI.

        You could do:

        #!/usr/bin/perl -w use strict; my $name = 'name'; my $home = "town"; my $form = "\@<<<<<<<<<<<<<<<<<< \@<<<<<<<<<<<<<<<\r\n"; formline($form, $name, $home); print $^A;
        See perlvar, formline for fiddling with the formats. But maybe it would be just easier to postprocess?
        #!/usr/bin/perl -w use strict; my $name = 'name'; my $home = 'town'; printknecht(); # let's fork us a scribe write; format STDOUT = @<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<< $name, $home . sub printknecht { return if my $pid = open(STDOUT, "|-"); # return if parent. die "cannot fork: $!" unless defined $pid; while (<STDIN>) { chop; print $_."\r\n"; } exit; }
        See Playing with STDIN and STDOUT for some prosa around this technique.

        --shmem

        _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                      /\_¯/(q    /
        ----------------------------  \__(m.====·.(_("always off the crowd"))."·
        ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

        $\ is the output record seperator for just the print operator (and not formats). You're probably better off using sprintf instead of formats.

        -derby
Re: formats output \r\n question
by clwolfe (Scribe) on Jun 28, 2006 at 05:16 UTC

    Or you could pipe the output of your script through unix2dos.

    But that's the lazy unix shell programmer in me coming out. Coming up with the right output to begin with is probably more right (as opposed to spawning a whole new process just for line endings :-)

Re: formats output \r\n question
by ioannis (Abbot) on Jun 27, 2006 at 18:57 UTC
    See perlrun; you can also use the -l option for end-line processing. This will affect print() and write(), but not funtions like printf() .

    #/usr/bin/perl -lw

Re: formats output \r\n question
by Ieronim (Friar) on Jun 28, 2006 at 13:39 UTC
    In perl 5.8.x you can use the following solution:
    #!/usr/bin/perl binmode STDOUT, ':crlf'; print STDOUT "town\n";
    All newline characters will be converted to CRLF.
Re: formats output \r\n question (@)
by tye (Sage) on Jun 28, 2006 at 05:54 UTC
    format STDOUT = @<<<<<<<<<­<<<<<<<<< @<<<<<<<<<­<<<<<<<<@ $name, $home, "\r" .

    - tye        

      Did you test that? Doesn't work. If it did, the OPs solution had been working in the first place.
      _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                    /\_¯/(q    /
      ----------------------------  \__(m.====·.(_("always off the crowd"))."·
      ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

        Yes, I did test it. I see now how my test was flawed. Thanks.

        This is another alternative, but I agree that I don't see what the advantage of this is over printf:

        binmode STDOUT; while( ... ) { my( $name, $home )= ...; formline "@<<<<<<<<<­<<<<<<<<< @<<<<<<<<<­<<<<<<<<\r", $name, $hom +e; print $^A, $/; $^A= ""; }

        This also suggested another "solution" that works but is quite undesirable due to it being fragile: Just include an extra literal carriage return on the end of each of the format's "picture" line(s) in the script's source code.

        - tye        

Re: formats output \r\n question
by Anonymous Monk on Jun 28, 2006 at 14:16 UTC
    Try it with HEX-Code 10 and 13
Re: formats output \r\n question
by Moron (Curate) on Jun 28, 2006 at 15:02 UTC
    People are probably more familiar with
    local $/ = 'modified input record separator';
    and yet another way to skin this cat is to use its brother:
    local $\ = chr(10) . chr(13);

    -M

    Free your mind