Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

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 (Monsignor) 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]
[stevieb]: ...on request. There's some supporting work I need to do, as well as update the docs, but it's in the v1.12 branch if anyone wants to play with it...
[stevieb]: ...issue 62 will track what else needs to be done.

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2017-03-31 01:34 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (364 votes). Check out past polls.