Full definition of backslash in double-quoted strings?

Discussion in 'Perl Misc' started by Tim McDaniel, Jun 9, 2014.

  1. Tim McDaniel

    Tim McDaniel Guest

    A cow-orker coded

    qq{... $id\_suffix ...}

    It has the effect of

    qq{... ${id}_suffix ...}

    I've looked thru "man perlop", specifically the section "Quote and
    Quote-like Operators". It defines what Perl does with many sequences
    (\n, \cX, \u, et cetera), but I don't see where it says anything about
    "For any sequence not described above". Am I overlooking it? If not,
    do you think I should ding my cow-order for the construct and ask him
    to change it to

    qq{... ${id}_suffix ...}

    ?

    --
    Tim McDaniel,
    Tim McDaniel, Jun 9, 2014
    #1
    1. Advertising

  2. (Tim McDaniel) writes:
    > A cow-orker coded
    >
    > qq{... $id\_suffix ...}
    >
    > It has the effect of
    >
    > qq{... ${id}_suffix ...}
    >
    > I've looked thru "man perlop", specifically the section "Quote and
    > Quote-like Operators". It defines what Perl does with many sequences
    > (\n, \cX, \u, et cetera), but I don't see where it says anything about
    > "For any sequence not described above". Am I overlooking it?


    perldata also mentions that "the usual C-style rules apply" (for
    backslash escapes), however, the 'usual C-style rules' require a
    diagnostic in case of unknown \-digraphs. The Bourne shell behaves in
    this way, however, where

    a <backslash> that is not quoted shall preserve the literal
    value of the following character

    > If not, do you think I should ding my cow-order for the construct and
    > ask him to change it to
    >
    > qq{... ${id}_suffix ...}
    >
    > ?


    My opinion on that would be "Yes" since using {} in this way is a
    documented feature while the other isn't.
    Rainer Weikusat, Jun 9, 2014
    #2
    1. Advertising

  3. with <ln4o2f$qhe$> Tim McDaniel wrote:

    *SKIP*
    > I've looked thru "man perlop", specifically the section "Quote and
    > Quote-like Operators". It defines what Perl does with many sequences
    > (\n, \cX, \u, et cetera), but I don't see where it says anything about
    > "For any sequence not described above". Am I overlooking it?


    Probably -- not. May be -- not yet.

    > If not, do you think I should ding my cow-order for the construct and
    > ask him to change it to


    Well, perlrebackslash suggests this reasoning:

    The rules determining what it is are quite simple: if the
    character following the backslash is an ASCII punctuation
    (non-word) character (that is,
    + anything that is not a letter,
    + digit, or underscore), then the backslash just takes away any
    + special meaning of the character following it.

    Note mentioning underscore.

    + If the character following the backslash is an ASCII letter or
    + an ASCII digit, then the sequence may be special;

    Now underscore is omitted.

    if so, it's listed below.
    + A few letters have not been used yet, so escaping them with a
    + backslash doesn't change them to be special.

    Right, ASCII doesn't have that many non-words Perl needs.

    A future version of Perl may assign a special meaning to them,
    so if you have warnings turned on, Perl issues a warning if you
    use such a sequence. [1].

    It is however guaranteed that backslash or escape sequences
    never have a punctuation character following the backslash, not
    now, and not in a future version of Perl 5. So it is safe to put
    a backslash in front of a non-word character.

    So, backslash takes away from non-words and gives to words. Surely,
    underscore is neither alpha nor digit. But it still stands for word
    anyway.

    *CUT*

    p.s. perlrebacklash seems irrelevant, right?

    This document describes all backslash and escape sequences. After
    explaining the role of the backslash, it lists all the sequences that
    have a special meaning in Perl regular expressions (in alphabetical
    order), then describes each of them.

    Or perl*re*backslash is just misleading?

    Most sequences are described in detail in different documents; the
    primary purpose of this document is to have a quick reference guide
    describing all backslash and escape sequences.

    p.p.s. My bet is your reasons (whatever those could be) will all fall
    dead.

    --
    Torvalds' goal for Linux is very simple: World Domination
    Stallman's goal for GNU is even simpler: Freedom
    Eric Pozharski, Jun 11, 2014
    #3
  4. Tim McDaniel

    Tim McDaniel Guest

    In article <>,
    Eric Pozharski <> wrote:
    >p.s. perlrebacklash seems irrelevant, right?
    >
    > This document describes all backslash and escape sequences. After
    > explaining the role of the backslash, it lists all the sequences that
    > have a special meaning in Perl regular expressions (in alphabetical
    > order), then describes each of them.


    I believe it is entirely irrelevant. It's talking only about the
    effect of backslashes in regular expressions. Not every sentence in
    that page includes "in regular expressions" or words to that effect,
    but that's the scope. In man perlop, which documents (to some extent)
    quoted string syntax, the only items listed in the table are \
    followed by an alphanumeric. In man perlop outside the table, there
    are a few other backslashed characters described outside regular
    expressions, most notably in the example output of \Q, but they are
    few and scattered -- \ followed by a string delimiter, for example, or
    \\ in different contexts. man perlop doesn't have (so far as I've
    seen) the nice statement that \alphanumeric is special and
    \non-alphanumeric is not.

    --
    Tim McDaniel,
    Tim McDaniel, Jun 12, 2014
    #4
  5. with <lnatho$p7r$> Tim McDaniel wrote:
    > In article <>,
    > Eric Pozharski <> wrote:


    >>p.s. perlrebacklash seems irrelevant, right?
    >>
    >> This document describes all backslash and escape sequences. After
    >> explaining the role of the backslash, it lists all the sequences that
    >> have a special meaning in Perl regular expressions (in alphabetical
    >> order), then describes each of them.

    >
    > I believe it is entirely irrelevant.


    Well, reason has fallen dead at that point. No problems.

    *CUT*

    --
    Torvalds' goal for Linux is very simple: World Domination
    Stallman's goal for GNU is even simpler: Freedom
    Eric Pozharski, Jun 12, 2014
    #5
  6. (Tim McDaniel) writes:
    > In article <>,
    > Eric Pozharski <> wrote:
    >>p.s. perlrebacklash seems irrelevant, right?
    >>
    >> This document describes all backslash and escape sequences. After
    >> explaining the role of the backslash, it lists all the sequences that
    >> have a special meaning in Perl regular expressions (in alphabetical
    >> order), then describes each of them.

    >
    > I believe it is entirely irrelevant.


    As written, yes, especially considering that some "regex constructs"
    don't make any sense outside of a regex. But this would rather seem a
    problem of incorrect/ incomplete documentation as backslashes are
    interpreted more liberally in quoted strings than the documentation
    suggests.

    Especially,

    [rw@sable]~#perl -we 'print "\z"'
    Unrecognized escape \z passed through at -e line 1.
    z[rw@sable]~#perl -we 'print "\_"'
    _[rw@sable]~#

    This suggests that \_ is safe (in a suitable context) despite being
    considered a word character.

    I'd nevertheless avoid using a \ as 'special-case syntax' for indicating
    that a _ following some other 'word characters' is not supposed to be
    part of the name of an interpolated variable as the documentation on
    that is surely unclear and 'saving a single character now and then' is
    not that much of a convenience.
    Rainer Weikusat, Jun 12, 2014
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sydex
    Replies:
    12
    Views:
    6,471
    Victor Bazarov
    Feb 17, 2005
  2. Harry George
    Replies:
    5
    Views:
    732
    Gerrit Holl
    Jan 19, 2004
  3. Vincent Texier
    Replies:
    3
    Views:
    2,262
    Vincent Texier
    Jul 1, 2004
  4. Fred
    Replies:
    2
    Views:
    367
    John Machin
    Mar 9, 2005
  5. david
    Replies:
    9
    Views:
    110
    cartercc
    Jan 21, 2009
Loading...

Share This Page