Full definition of backslash in double-quoted strings?

T

Tim McDaniel

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

?
 
R

Rainer Weikusat

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

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

Eric Pozharski

*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.
 
T

Tim McDaniel

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

Rainer Weikusat

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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,787
Messages
2,569,630
Members
45,335
Latest member
Tommiesal

Latest Threads

Top