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

I've been looking at some code, but can't figure out what \1 does when not used in a regexp.
Here is a sample:

$n = "\1r\1n";

Thanks

Replies are listed 'Best First'.
Re: What does \1 do?
by cciulla (Friar) on May 10, 2003 at 12:13 UTC
      Using a backslash "escapes" the following character.

      That's not entirely true in this case: if the first character following a backslash is a digit [0-7], the sequence of up to 3 octal digits following is the escape sequence, and the resulting octal number is converted to the character with that index:

      perl -wle '$s = "\100"; print ord($s)' 64

      Similarly, "\x" introduces a sequence of up to 2 hex digits, eg "\x3f". In Unicode-aware perls (perl-5.6.0 and later) you can also introduce codepoints above 255 by putting braces around the hex number:

      perl -wle '$s = "\x{1000}"; print ord($s)' 4096

      Hugo