http://www.perlmonks.org?node_id=812175


in reply to Interpretting character combinations as special characters.

$text = 'h\te\nll\t\(o\)\n'; print "$text\n"; $text = eval '"'.$text.'"'; print "$text\n";

If not already escaped, you probably have to escape backslashes, double quotes, dollar signs, and some other special chars... Don't know what is worst!

Replies are listed 'Best First'.
Re^2: Interpretting character combinations as special characters.
by music_man1352000 (Novice) on Dec 10, 2009 at 13:32 UTC
    @vitoco: I know what you mean! However your suggestion should actually work perfectly for me because I am reading the input character by character (which means I can do eval's only where I find \'s and not have to escape $, @ % etc.).

    @all: thanks once again for all your help!
Re^2: Interpretting character combinations as special characters.
by afoken (Chancellor) on Dec 10, 2009 at 20:12 UTC

    String eval for arbitary strings? A very optimistic approach. Escaping all those characters that can make perl execute malicious code is surely more work than the other solutions. And if the OP did not understand or ignored the text below your posting, you just made him insert a gapping security hole into his code.

    So: --

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

      You are right... I should have begun with a big warning about how evil could eval be.

      Fortunately, OP is processing one char at a time, and I couldn't think of a one-byte malicious code. ;-)

      BTW, the first solution I though was this:

      $text = 'h\te\nl\l\t\(o\)\n'; print "$text\n"; %token = ('\n'=>"\n", '\t'=>"\t" , '\('=>'(', '\)'=>')'); $text =~ s/(\\.)/$token{$1}||$1/ges; print "$text\n";

      That will preserve unknown escape codes from text...

      Also, thanks to JavaFan, // operator was new to me...