theNoobExpert
theNoobExpert

Reputation: 178

What is the purpose of "\c" in perl?

What does \c do in perl? I tried looking for it everywhere but the support for perl is not so good as compared to other languages. Everywhere that i looked it showed that it is a control statement and its syntax is \cX where x can be any character. But that doesn't explain anything. So can anyone please explain what does it do and how to use it?

Upvotes: 3

Views: 1209

Answers (1)

Jonathan Leffler
Jonathan Leffler

Reputation: 753655

Transferring comments to an answer.

Try:

perl -l -e 'print "\cC"'

It prints a control-C and a newline (you might need to pipe it to od -c or xxd -g 1 or some similar data dumper). Now what do you think '\cX' means?

The documentation doesn't stipulate alphabetic only because the ASCII code set has @ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_ as consecutive character codes, and you can use, for example:

perl -l -e 'print "\c_"'

to print 0x1F or control-underscore or 'unit separator', etc. You can use lower-case or upper-case letters, but only the non-alphabetic characters shown ("\c@" is the null byte, '\0' in C). Note that the backslash needs special treatment. The escape sequence cannot appear at the end of a string.

perl -l -e 'print "\c\X"'

This prints 0x1C (control-backslash) followed by X and a newline. I guess that if you need that at the end of a string, you'll have to use a substring operation:

perl -l -e 'print substr("\c\X", 0, 1)'

You can also use \c? to print the DEL character, 0x7F, on ASCII-based platforms (see the manual for what it means under EBCDIC if that is a problem for you, or your curiosity is piqued).

As mentioned byHåkon Hægland in a comment, you can find the details under Quote and quote-like operators in the perlop documentation.

Upvotes: 9

Related Questions