interpolate escaped chars in string 
Author Message
 interpolate escaped chars in string

Hello, all.

A few months ago, there was a thread about how to interpolate character
sequences the way perl would in a double-quoted string. For example, how
to take 'Hello, world\n' and interpolate the '\n' so that the string
becomes "Hello, world\n".

From my dim recollection, and a bit of experimentation, I came up with
this:

    $str =~ s/(\\[nrtv])/'"'.$1.'"'/ee

where the character class contains only the \-sequences that I want to
interpolate.

My questions are:

1) Is there a more efficient way, possibly avoiding the double-eval?

2) Are there any security implications of using the /ee ? From my
   limited overview, I can't see any, since only my chosen four strings
   can possibly be eval'd.

Thanks,
Gopi.

--
Gopi Sundaram



Tue, 28 Sep 2004 14:45:42 GMT  
 interpolate escaped chars in string


| A few months ago, there was a thread about how to interpolate character
| sequences the way perl would in a double-quoted string. For example, how
| to take 'Hello, world\n' and interpolate the '\n' so that the string
| becomes "Hello, world\n".

There has been more than one such thread in the past weeks :)

| From my dim recollection, and a bit of experimentation, I came up with
| this:
|
|     $str =~ s/(\\[nrtv])/'"'.$1.'"'/ee
|
| where the character class contains only the \-sequences that I want to
| interpolate.
|
| My questions are:
|
| 1) Is there a more efficient way, possibly avoiding the double-eval?

Well, you can always use eval '"' . $str . '"'
However, that will also interpolate var names in the string and it may

| 2) Are there any security implications of using the /ee ? From my
|    limited overview, I can't see any, since only my chosen four strings
|    can possibly be eval'd.

ee by itself doesn't have any security problems, as far as I know. However,
you'd want to add a g to the ee, right? And you'd want to interpolate \\
into \ to make '\n' a possible result.

HTH,
Steffen
--
$_=qq#tsee      gmx.net#;s#e#s#g;s#[^\s\w]#c#;s#s#ust#g;s#t#J#e;s#nus#ker#





Tue, 28 Sep 2004 15:02:48 GMT  
 interpolate escaped chars in string

Quote:

> Hello, all.

> A few months ago, there was a thread about how to interpolate
> character sequences the way perl would in a double-quoted string. For
> example, how to take 'Hello, world\n' and interpolate the '\n' so that
> the string becomes "Hello, world\n".

> From my dim recollection, and a bit of experimentation, I came up with
> this:

>     $str =~ s/(\\[nrtv])/'"'.$1.'"'/ee

> where the character class contains only the \-sequences that I want to
> interpolate.

Methinks you wanted /gee, not /ee.

Also, what happens if you want a literal backslash before an ordinary n?

Quote:
> My questions are:

> 1) Is there a more efficient way, possibly avoiding the double-eval?

Sure.
my %esc = ( n => "\n", r => "\r", t => "\t", v => "\v", "\\" => "\\" );
$str =~ s/\\([nrtv\\])/$esc{$1}/g;

Quote:
> 2) Are there any security implications of using the /ee ? From my
>    limited overview, I can't see any, since only my chosen four
>    strings can possibly be eval'd.

There are security implications of using /ee *in general*, but in your
specific case, because you have carefully chosen your strings, it's
safe.

--
print reverse( ",rekcah", " lreP", " rehtona", " tsuJ" )."\n";



Thu, 30 Sep 2004 04:08:01 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. reading char by char in a string

2. Char position of 1st non-word char in a string

3. Appending char to strings by char

4. escape special chars before loading into MySQL

5. Adding escape chars to email address

6. backslash escaping not metacharacters in a char class

7. is there a way to escape special chars

8. which chars to escape in reg.exp?

9. Problem with escaped octal chars > 127

10. Newbie - Escaping special chars in forms

11. split a string not only by a single char but also by a string

12. Newbie: sprintf ('%-20s', 48 char string) returns 48 not 20 length string

 

 
Powered by phpBB® Forum Software