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, Jun 9, 2014
    1. Advertisements

  2. 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

    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
    1. Advertisements

  3. *SKIP*
    Probably -- not. May be -- not yet.
    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


    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
    Eric Pozharski, Jun 11, 2014
  4. Tim McDaniel

    Tim McDaniel Guest

    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, Jun 12, 2014
    Eric Pozharski, Jun 12, 2014
  6. 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


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

    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
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.