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

im am learning perl and wonder if somone can answer these 2Q's for me.

1. when would you use a \n over a \r?

2. how would i make the following line appear on its own line?

<print 'I a new line after this statement';>
Edited 2001-04-23 by Ovid

Replies are listed 'Best First'.
Re: easy newbie question
by murphya (Sexton) on Apr 23, 2001 at 23:51 UTC
    Really your best bet is to get a decent book, which will explain these things (and obviously more advanced stuff!)

    But for now,

    (1) Nearly always.

    (2) Not too sure about the context of your case since the question is not clear, but try:

    print "\n This will be on a line by itself.\n";

    Buy that book if you can, Programming Perl published by O'Reilly is probably best.

    Good Luck

      Based on original question, I suspect that Learning Perl might be a better choice here as it is geared more toward the "beginner" end of the programming experience scale.

              - tye (but my friends call me "Tye")
      mmm..ok

      I'm a newbie but im not stupid. At least i dont think i posted that its a "stupid newbie question". I would not ask if it was in some documented form readily available for me. I have 4 books on perl and not one answer this question.

      I printed the line like i did for a reason. It was with single quotes not double. I just wanted to know how you would put a new line if you didnt have the double quotes. Its not in all 4 books i have and it might be that "you would never use the single quotes if you want to make a new line" but i cant find that statement and wish to know this before i move on.

      So one more time. ---How would you get this to print a line after it-- ------------------------------------------------------- -----print 'This will be on a line by itself.';-------- ------------------------------------------------------- 1. is the only way with double quotes? 2. can you get a new line with single quotes?
      as for "Programming Perl published by O'Reilly "--there is nothing newbie about this book. O'Reilly is good but they dont cater to beginners.

      Edit by tye

        Double quotes interpolate, which means that variables get expanded within them, and escaped characters like \n and \r are interpreted as special. Single quotes are intrepreted literally, so

        print "$string\n"; #(prints the current value of $string followed by a + newline) print '$string\n' # prints out the characters $, s, t, r, i, n, g, \, +n

        (Update see String Literals in Perl for more info. We now return you to the original message) You may be relieved to find out that

        print 'This must appear on its own line ';

        Sticks a newline into the printed string. And Learning Perl, from O'Reilly, certainly does cater to beginners; moreover, there is Learning Perl on Win32 Systems from the same publisher, and if you have something against Sebastopol, CA, Elements of Programming With Perl is out there.

        When do you *need* to explicitly output \r ? I don't think one usually needs to worry about that under NT/2K. I do believe that Perl is smart enough to add it for you when needed. Unfortunately, I can't check that just now ... (I'm too much of a *nix guy these days)

        Hiya, rimuladas.

        I wanted to let you know that I did downvote your node, for a couple of reasons.

        • nobody called you stupid, hombre. we generally dont do that here.
            <print 'I a new line after this statement';>
        • doesnt even appear to be in perl. is that javascript? and yet, people attempted to help you. what irked me is that you got hostile and defensive about things.

        Programming Perl is not intended to be a book for newbies. I read Programming Perl 1st edition and was truly lost, even though I knew quite a bit of C and a few other languages at the time. A couple years later, I came back to it, and decided instead to purchase the second edition and Learning Perl as well. O'Reilly has excellent books, and they even do a good job at teaching newbies. Programming The Perl DBI (by alligator descartes) is an excellent example of this, as is Learning Perl.

        arturo gave you the correct answer. There are also several other threads that deal with this. I encourage you to use the Super Search and, if youre 'leet, the Secret Search <grin>.

        finally, i want to restate that we're here to help, and i think that 99% of the time, we manage to be a very good, generous lot. so be respectful.

        brother dep.

        --
        Laziness, Impatience, Hubris, and Generosity.

Re: easy newbie question
by repson (Chaplain) on Apr 24, 2001 at 09:46 UTC
    The only time you should use "\r" is when you specifically require a carrige return without a new line, like in this.

    for (1..10) { print "\r$_"; sleep 1; }

    That code counts from one to two once per second reusing the same line. This can be useful for a progress counter or other ticker you need to update.

    In almost all other cases a "\n" is the normal way for new lines, except in network communication where you should generally use "\015\012" or something similar.