Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re: Interpretting character combinations as special characters.

by vitoco (Friar)
on Dec 10, 2009 at 13:18 UTC ( #812175=note: print w/replies, xml ) Need Help??

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 (Abbot) 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: --


    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...

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://812175]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2018-01-23 14:37 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (248 votes). Check out past polls.