Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

print, put, say and escape characters

Tags:

string

raku

According to the docs, the only difference between print and say seems to be the fact that the latter adds "\n" (and stringifies using .gist). However,

perl6 -e 'print "del\b\b"'

prints "d", effectively applying escape characters, while

perl6 -e 'put "del\b\b"'

will output "del" (the same as say). Is it possible that there's a third way of stringifying strings, besides .gist and simple .Str?

As a matter of fact, any character behind the \b will make them behave in the same way. So any idea of why this happens?

like image 504
jjmerelo Avatar asked Jan 10 '19 09:01

jjmerelo


People also ask

How do I print an escape character?

We have many escape characters in Python like \n, \t, \r, etc., What if we want to print a string which contains these escape characters? We have to print the string using repr() inbuilt function. It prints the string precisely what we give.

How do I print a string without escape characters?

Using “r/R” Adding “r” or “R” to the target string triggers a repr() to the string internally and stops from the resolution of escape characters.

How do you escape a print in Python?

\ " and \ ' prints double and single quote respectively. We need to use a double backslash (\) to print a backslash () as \ is an escape character.

What is escape character in Python?

In Python strings, the backslash "\" is a special character, also called the "escape" character. It is used in representing certain whitespace characters: "\t" is a tab, "\n" is a newline, and "\r" is a carriage return.


2 Answers

FWIW, I see "del" in both cases, regardless of using print or put, so maybe there is some terminal setting that is affecting the behaviour?

The \b\b only become obvious when you actually put characters after them:

say "del\b\bo the right thing"  # do the right thing

\b only moves the cursor back one position. It does not erase anything by itself. If you want the characters erase, you'd have to have them followed by spaces, and then backspace again if you want any text after that again:

print "del\b\b  \b\b"           # d
like image 111
Elizabeth Mattijsen Avatar answered Oct 21 '22 05:10

Elizabeth Mattijsen


To see what's being output, without possible confusion of what the terminal might be doing, you can pipe through xxd (or od)

$ perl6 -e 'say "del\b\b"' | xxd
00000000: 6465 6c08 080a                           del...
$ perl6 -e 'print "del\b\b"' | xxd
00000000: 6465 6c08 08                             del..
$ perl6 -e 'put "del\b\b"' | xxd
00000000: 6465 6c08 080a                           del...
like image 27
Norman Gaywood Avatar answered Oct 21 '22 04:10

Norman Gaywood