Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Escape characters question

by hweefarn (Acolyte)
on Dec 29, 2003 at 07:28 UTC ( [id://317373] : perlquestion . print w/replies, xml ) Need Help??

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

hi :)

recently i've been learning about using perl to send email.
i try to look at some examples that i found from the internet.
in some example, i observed there is something like scripts below:
if ( my $server = <$sock>) { print "Got Handshake: $server"; dialog( $sock, "HELO $forged_from\015\012" ); dialog( $sock, "MAIL FROM: $from\015\012" ); dialog( $sock, "RCPT TO: $to\015\012" ); dialog( $sock, "DATA\015\012" ); dialog( $sock, "$msg\015\012.\015\012" ); dialog( $sock, "QUIT\015\012" ); }

so, i would like to ask, what is the meaning of "\015\012" ?
is it same with "\r\n"? i see some of the example using \r\n

by the way, what is the meaning of \r? i know that \n means go to next line.

thank you very much :)


Edit by castaway added code tags.

janitored by ybiC: Retitle from "simple question" for future search-friendlyness

Replies are listed 'Best First'.
Re: Escape characters question
by simonm (Vicar) on Dec 29, 2003 at 07:58 UTC
    what is the meaning of "\015\012" ? is it same with "\r\n"?

    The \015\012 means "CR-LF", a carriage-return character followed by a line-feed character.

    The \r\n is intended to produce the same results, but the value of \n can change depending on your operating system, so for network protocols where you specifically mean to send CR-LF, the \015\012 sequence is your best bet.

    by the way, what is the meaning of \r? i know that \n means go to next line.

    A long time ago, on a minicomputer line printer far far away, a CR shifted the print head all the way back to the left end of the line, and LF shifted it down one line, thus the names carriage return and line feed. Modern systems use either one or the other, or both, to separate lines of text.

Re: Escape characters question
by !1 (Hermit) on Dec 29, 2003 at 07:58 UTC

    $forged_from? ....

    \015 and \012 are octal escape sequences with the equivalent decimal being 13 and 10. They represent \r and \n, respectively, on most systems. That's why \015 and \012 are used; they're the characters you want whereas \r and \n aren't always depending upon OS. More info in perldoc perlop

    \r is return or carriage return (CR). \n is newline (NL). There are a few places on the internet describing the difference and why it's important to know which or what combination to use.

Re: Escape characters question
by jeffa (Bishop) on Dec 29, 2003 at 14:43 UTC
Re: Escape characters question
by revdiablo (Prior) on Dec 29, 2003 at 08:14 UTC

    \015 is equivalent to \r and \012 is sometimes equivalent to \n. The sometimes is the reason people often use the octal codes instead of the symbolic versions. Depending on a number of things I'm not quite sure about (I think the primary thing is what operating system you're using), \n can return different things (\012, \015\012, and maybe even sometimes \015). Since a goal of writing good network applications is following protocol specifications as closely as possible, some coders find the extra precision granted by using the octal codes to be worth the (imho) ugliness. I'll leave the decision up to you. 8^)

    As for the last question, \r is carriage return. Think of a typewriter -- there are two distinct operations when you whack the Return key. First the paper scrolls up a little bit, then the type head returns to the far-left position. The first operation is called line feed, and the second is called carriage return (you might now understand why \r\n newlines are also called CRLF, if you've ever run into that initialism).

Re: Escape characters question
by Roy Johnson (Monsignor) on Dec 29, 2003 at 17:06 UTC
    If, like me, you find it easier to think in terms of control characters, these can be represented thus:
    Carriage return Control-M \cm
    Line feed Control-J \cj

    The PerlMonk tr/// Advocate