#1
  1. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,436
    Rep Power
    652

    Single Quote Question


    I'm working with active state perl and my question is why does this:
    Code:
    print('\\');
    Produce this output?
    Code:
    \
    Instead of:
    Code:
    \\
    TIA.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Location
    Paris area, France
    Posts
    846
    Rep Power
    499
    Because the backslash if the escape character changing the value of the next character. For example "\n" if for a new line, "\r" is for a carriage return, etc.

    Therefore if you want to use really the literal backlash character, you need to escape it to indicate that you want to use the literal value of the backslash, and you escape it by putting another backslash immediately before. So you use "\\" for printing one backslash, and "\\\\" for printing two.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,436
    Rep Power
    652
    Disagree. Single quotes are supposed to ignore escapes.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  6. #4
  7. !~ /m$/
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    May 2004
    Location
    Reno, NV
    Posts
    4,274
    Rep Power
    0
    The reason is because perl allows you to escape a single quote character so you can have one within a single-quoted string.

    Special Characters in Single-quoted Strings

    There are two characters in single quoted strings that do not always represent themselves. This is due to necessity, since single-quoted strings start and end with the ' character. We need a way to express inside a single-quoted string that we want the string to contain a ' character.

    The solution to this problem is to preceded any ' characters we actually want to appear in the string itself with the backslash (\ character). Thus we have strings like this:

    'xxx\'xxx'; # xxx, a single-quote character, and then xxx

    We have in this example a string with 7 characters exactly. Namely, this is the string: xxx'xxx. It can be difficult at first to become accustomed to the idea that two characters in the input to Perl actually produce only one character in the string itself. (C programmers are already probably used to this idea.) However, just keep in mind the rules and you will probably get used to them quickly.

    Since we have used the \ character to do something special with the ' character, we must now worry about the special cases for the backslash character itself. When we see a \ character in a single-quoted string, we must carefully consider what will happen.

    Under most circumstances, when a \ is in a single-quoted string, it is simply a backslash, representing itself, as most other characters do. However, the following exceptions apply:

    The sequence \' yields the character ' in the actual string. (This is the exception we already discussed above).

    The sequence \\ yields the character \ in the actual string. In other words, two backslashes right next to each other actually yield only one backslash.

    A backslash, by itself, cannot be placed at the end of a the single-quoted string. This cannot happen because Perl will think that you are using the \ to escape the closing '.
    Newlines in Single-quoted Strings

    Perhaps perl should not have allowed quotes within quotes, but there are always compromises.

    Comments on this post

    • Laurent_R agrees : Very good and complete answer.
    Last edited by keath; June 18th, 2014 at 07:40 PM.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Location
    Paris area, France
    Posts
    846
    Rep Power
    499
    Originally Posted by gw1500se
    Disagree. Single quotes are supposed to ignore escapes.
    Not always.

    I was going to explain further, but Keath has given a very good and complete explanation.

IMN logo majestic logo threadwatch logo seochat tools logo