Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

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!


Comment on Re: Interpretting character combinations as special characters.
Download Code
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 (Parson) 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...

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://812175]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (14)
As of 2014-12-18 22:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (67 votes), past polls