Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Re: Re: An introduction to POE

by rob_au (Abbot)
on Aug 10, 2003 at 13:23 UTC ( [id://282611]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: An introduction to POE
in thread An introduction to POE

I'm QUITE sure that using \012\015 is a bad idea ... \n\r has a chance of doing the right thing.

Um, I don't think so - Read the perlport man page under the heading "Newlines". To quote:

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.

print SOCKET "Hi there, client!\r\n"; # WRONG print SOCKET "Hi there, client!\015\012"; # RIGHT

This is equally applicable to the use of the literal \r and \n in general programming.

 

perl -le 'print+unpack"N",pack"B32","00000000000000000000001001111000"'

Replies are listed 'Best First'.
\r\n vs \012\015, was Re(3) An introduction to POE
by RMGir (Prior) on Aug 10, 2003 at 13:48 UTC
    (We're getting pretty far off the topic of POE here...)

    You are correct for network protocols. Of course, at that level it's important that the correct bytes are sent, if you're doing raw FTP or RSH, for instance.

    But for terminal interactions, you have to have "the right thing" happen. And what does the right thing can be different from platform to platform. In those cases, I think \n and \r are the correct choices.

    Even if you're accessing another system remotely, it's the terminal driver on that system that will make the decision how to translate local \n into the right thing on the wire.
    --
    Mike

      Even when it comes to network protocols, "\r" and "\n" can be a better choice than "\012\015". In fact, the only reason this "\012\015" has become popular is the old Macintosh! If it weren't for that, then there would be no case where "\012\015" was a better choice.

      Consider running Perl on an EBCDIC system (yes, there are such ports of Perl) trying to talk SMTP to send mail. You need to say "HELO\n" but try to be "portable" and say "HELO\012". You think the SMTP server you are talking to is going recognize "HELO" in EBCDIC?? Of course not. Which means that on an EBCDIC system talking over some socket emulation, you are going to be going through a EBCDIC/ASCII converting gateway and if you send "\012" through that from the EBCDIC side, it isn't going to reach the other side as "\n". You want "HELO\n".

      It is sad that the old Mac mistakes have so confused people about what is portable code. "\012\015" is portable to the old Mac but is very specific to the near-ASCII nature of old Macs. Using such without first testing whether you are on a system where it works just indicates that you don't care about alternate character encodings. ASCII has become so popular that you can probably justify this sometimes.

      But I find it silly that people proclaim how this makes their code more portable. It is just an old Mac thing. I'm not sure what non-ASCII environments Perl will end up running on; probably not very many. But hard-coding magic numbers is just not the way to go.

      See Re^4: Line Feeds (rumor control) for if you want more on this. (:

                      - tye
        Actually if you want it to work across platforms you need to match (\r?\n|\015?\012)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (6)
As of 2024-04-19 11:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found