Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^3: problem with 'bare LF' in script

by monarch (Priest)
on Nov 12, 2008 at 00:57 UTC ( [id://723038]=note: print w/replies, xml ) Need Help??


in reply to Re^2: problem with 'bare LF' in script
in thread problem with 'bare LF' in script

No idea if this will help:
$str =~ s/(\r\n|\n\r|\r|\n)/\r\n/sg;
where \r is a carriage-return and \n is a line-feed. This code will convert every type of line ending to a CR-LF pair.

Update: removed parenthesis from replacement text

Replies are listed 'Best First'.
Re^4: problem with 'bare LF' in script
by GrandFather (Saint) on Nov 12, 2008 at 02:49 UTC

    See the Newlines section in perlport for why using \r and \n is a bad idea™ in this sort of context.


    Perl reduces RSI - it saves typing

      I've read:

      A common misconception in socket programming is that \n eq \012 everywhere. When using protocols such as common Internet protocols, \012 and \015 are called for specifically, and the values of the logical \n and \r (carriage return) are not reliable.

      before, and stuff around it, and I still don't understand :-(

      Is this telling me that when Perl interpolates the string "\r\n" that it may use some other values for the two escapes ? If so, where ? I've not been able to catch it doing that, but I have only got Winders and LINUX to go on. Does "\n" interpolate to "\x0D" on a Mac ?

      I understand that IO layers may translate "\x0A" bytes to and from "\x0D\x0A" pairs and vice versa. I guess there's similar support for "\x0D" line-endings (though I haven't found it). I can see that means that if you think of "\n" as interpolating to a local system newline, you may be disappointed. But that's not the same as "\n" interpolating to something different in different places or at different times.

      Desperately seeking enlightenment....

        A key quote from the documentation is "Perl uses \n to represent the "logical" newline" which implies that \n may not be \x0a. It then goes on to say that in MacPerl \n is \x0d.

        By default Perl's text IO layers translate between the host OS's notion of a line end sequence and the character used internally to represent \n. If a string being manipulated hasn't been through an appropriate IO layer you will be dealing with the original CR and LF characters that were used. In various situations that means that an interpolated \n will not match LF (\x0a) and in many more situations it will not match an actual line end sequence (in Windows in particular).

        As with many things in Perl, the default behavior is almost always what you want. So often so in fact that you tend to forget that there might be any other behavior.


        Perl reduces RSI - it saves typing

        Does "\n" interpolate to "\x0D" on a Mac ?

        Yes, on old MacPerl. Not anymore, though. (Not since OS X in 1999?)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (3)
As of 2024-04-20 01:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found