in reply to String::Escape
in thread efficient char escape sequence substitution

I've been trying out String::Escape.
The docs show using double backslash escapes however it appears to work the same with single backslashed r, t and n.
However, it does not appear to work with hex or octal escapes.

erickn@cosmora01d:/home/erickn/String-Escape-2002.001/blib/lib> cat x use lib '.'; use String::Escape qw(unprintable); $var = 'this\tis\ta\011string\x09with some text\r\n'; print unprintable($var); $var = 'this\\tis\\ta\\011string\\x09with some text\\r\\n'; print unprintable($var); erickn@cosmora01d:/home/erickn/String-Escape-2002.001/blib/lib> perl x this is a1string\x09with some text this is a1string\x09with some text

Am I using it incorrectly?

Replies are listed 'Best First'.
Re^2: String::Escape
by Animator (Hermit) on May 27, 2005 at 20:18 UTC

    Try printing the value of $var... it will show something you don't expect... \\ in single quotes still escapes the backslash and results in only one backslash.

    Converting the hex-strings can be done like this: s#(?<!\\)(\\{2})*\\x([A-F0-9a-f]{2})#$1 . chr (hex ($2))#eg;

    It use a look-back to see that there is no backslash before the double backslashes, then it matches two backslashes (a backslash escaping a backslash), and then the backslash and the x, and ofcourse the hex-symbols.

    If you want to have the correct result after that you should replace all double backslashes with a single backslash... or better said, remove the backslash before every symbol... but perhaps unprintable does that too...

    Update: I decided to look at the source of String::Escape, and the code to match the hex charachters seems to be bugged... as in, it does match \AF (basiclly m/\\[A-Fa-f0-9]/), but not with the \xAF before it... It doesn't seem to have code for octal charachters though... but you should be able to work that out with my previous regex (which is why I leave it in this post). (I will send a mail to the author about this...)

    Update2: Reply from the author:

    Looks like you're right. I've gotten a few other suggestions, so I'll try to roll them into a new release soon.

    Update3: I noticed that [id://djohnston] regex had a flaw and I explained to him how to fix it. After looking back at the original regex (of the OP), I noticed that it has the same flaw. So here is the explanation. (in readmore tags ofcourse)

Re^2: String::Escape
by marnanel (Beadle) on May 27, 2005 at 19:40 UTC
    Oh, no: I don't think it does hex or octal escapes. Sorry. It ought to!

    Maybe you could base something that does on the String::Escape code, though.