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

Hi

I have a hard time finding this behaviour documented in perldoc.

The following example is showing that "\x" and "\xx" are interpreted as octal code points in ASCII when used in string interpolation ( with x in 0..7)

DB<25> for (0..20) { $a= eval qq{"\\$_"}; print "\\$_: ",ord($a),"\n +";} \0: 0 \1: 1 \2: 2 \3: 3 \4: 4 \5: 5 \6: 6 \7: 7 \8: 56 \9: 57 \10: 8 \11: 9 \12: 10 \13: 11 \14: 12 \15: 13 \16: 14 \17: 15 \18: 1 \19: 1 \20: 16

The best I found is: (but only after realising it's octal)

perlrebackslash#Octal%20escapes

> In some contexts, a backslash followed by two or even one octal digits may be interpreted as an octal escape, sometimes with a warning, and because of some bugs, sometimes with surprising results.

Please note that perlrebackslash is supposed to describe escape sequences in regexes and not in general string interpolations.

Additionally does s/(PATTERN)/\1/ have a very different meaning in regexes (backreference of a matching group)

Is there a better perldoc to find the behaviour in normal "strings" explained?

Any canonical / obvious starting point for "string interpolation"?

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

nota bene

non octal sequences in the above example (like "\8" and "\18") will create warnings (if enabled).

Replies are listed 'Best First'.
Re: Perldoc: String interpolation with octal escpape sequence like \1
by choroba (Cardinal) on Oct 02, 2018 at 15:13 UTC
    perlop under Quote and Quote like Operators

    The following escape sequences are available in constructs that interpolate, and in transliterations:

    ...

    \033 [7,8] restricted range octal char (example: ESC)

    ...

    Some contexts allow 2 or even 1 digit, but any usage without exactly three digits, the first being a zero, may give unintended results.

    ...

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      Also:

      Starting in Perl 5.14, you may use \o{} instead, which avoids all these problems. Otherwise, it is best to use this construct only for ordinals \077 and below, remembering to pad to the left with zeros to make three digits.
      Basically "Don't do it". The confusion with \1 and so on being the main reason IMHO.

        > Basically "Don't do it".

        "Don't do it"* doesn't mean document it badly, especially if there is no warning.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

        *) BTW I agree!

Re: Perldoc: String interpolation with octal escpape sequence like "\1" (perlquote)
by Anonymous Monk on Oct 05, 2018 at 01:46 UTC

    Is there a better perldoc to find the behaviour in normal "strings" explained? Any canonical / obvious starting point for "string interpolation"?

    Start with perlintro then onto perlquote ;)