interpolate escaped chars in string
Author |
Message |
Gopi Sundara #1 / 3
|
 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 |
|
 |
Steffen Mülle #2 / 3
|
 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 |
|
 |
Benjamin Goldber #3 / 3
|
 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 |
|
|
|