Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
Think about Loose Coupling
 
PerlMonks  

formats output \r\n question

by egunnar (Sexton)
on Jun 27, 2006 at 15:20 UTC ( #557818=perlquestion: print w/ replies, xml ) Need Help??
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;

Comment on formats output \r\n question
Download Code
Re: formats output \r\n question
by shmem (Canon) 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; }

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

        -derby
        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}
Re: formats output \r\n question
by ioannis (Curate) 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 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 tye (Cardinal) 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 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 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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://557818]
Approved by Corion
Front-paged by ryantate
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (11)
As of 2014-04-17 09:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (443 votes), past polls