Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

CR+LF

by Gorio3721 (Acolyte)
on Jun 19, 2003 at 00:13 UTC ( [id://267033]=perlquestion: print w/replies, xml ) Need Help??

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

This should be an easy one but I am stumped.

I have a script that generates an init file for the fvwm window manager (running under cygwin).

The script worked just fine using the cygwin build of perl (5.8.0.3 I think). I switched over to ActiveState perl and the script seems to run just fine (The output looks the same) but it creates an output file with both CR+LF terminating each line instead of just the the LF line terminator that the cygwin build produced.

I think that fvwm barfs when it sees CR+LF in it's config file.

How do I coax perl to just print only the LF on stdout?

NOTE: I'm only printing to standard out and not explicitly opening the output file (for now) but I'm willing to change that.

Thanks! Gorio

Replies are listed 'Best First'.
Re: CR+LF
by jsprat (Curate) on Jun 19, 2003 at 02:07 UTC
    Here's a long answer, use one of the following:
    1. Explicitly print your line ending - use chr(13) or "\x0d" or "\015" or "\cM" instead of "\n"
    2. Convert the file from dos2unix using your favorite utility.
    3. In memory, perl sees no "\r\n" - only on output does it get translated. "binmode STDOUT; #any filehandle, not just STDOUT" prevents this translation.
    4. Change the output record separator , and print no explicit line ending, ie $\ = chr(13); print "This is my line"; #no line ending
    5. If the file is transferred by ftp, be sure it is ftp'ed in ascii mode

    Short answer - use method 1 if the file is only to be used on *nix platforms. Use 2 if you need the file on both platforms.

    HTH...

    BTW, more than once I've wished for a command line switch (or configuration variable) to specify the value of the newline character.

    Update: I always get cr & lf backwards - is it 10 and 13 or 13 and 10 :(

    And thank you Tye for the pointer to your Line Feeds node - printing \0x0A puts a \r\n into the file!

      Items 1, 2, and 4 don't solve the stated problem. They result in only CR on the ends of lines (not in only LF on the ends of lines).

      Also, your not mentioning "\r" in item 1 makes me think that you may have been confused (like so many) by the '"\n" is logical newline' idea that I find horrid. See Re^4: Line Feeds (rumor control) for more on that.

                      - tye
Re: CR+LF
by Thelonius (Priest) on Jun 19, 2003 at 03:25 UTC
    binmode STDOUT (or whatever file)
Re: CR+LF
by svsingh (Priest) on Jun 19, 2003 at 00:54 UTC

      Actually, I don't think this will work because he isn't trying to find CR/LF's in a file and replace with just LF's, he's talking about printing to stdout. The Win32 ActiveState build of Perl actually outputs a CR/LF pair when you try to print "\n". I don't have a Win32 machine handy to test on, but I can think of a couple possibilities:

      • Instead of using \n use \x0A (just an ASCII char 10, instead of the "symbolic newline", which gives a ASCII 13/ASCII 10 pair): print "Hi there.\x0ANew line here.";
      • Try doing binmode STDOUT; I know you can use that when printing to files so the symbolic newline isn't reinterpreted, and as far as I know STDOUT is just a regular filehandle that you can call binmode on.
      • If neither of those work, try writing to a file instead of directly to stdout. Then you can definitely use binmode and avoid getting CR/LF pairs.

      kelan


      Perl6 Grammar Student

        Oh wow. Nice catch. Sorry for sending you down the wrong path Gorio. Thanks for your insights Kelan.
Re: CR+LF
by bm (Hermit) on Jun 19, 2003 at 09:54 UTC
    Some good background info can be found in the Newlines section of perlport

Re: CR+LF
by Gorio3721 (Acolyte) on Jun 19, 2003 at 16:09 UTC

    I thank all of you who responded on this.

    As usual, there is no better place to get answers I need (and that work)

    Gorio

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://267033]
Approved by kelan
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (8)
As of 2024-04-19 07:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found