Rob Dixon wrote:
>
> Peter Daum wrote:
>>
>> I am looking for an way to interpolate backslash-sequences
>> within a string with the usual perl semantics, e.g.
>> $s='1\t\2\t3\na\ b c' should become:
>> '1<tab>2<tab>3
>> a b c'
>>
>> Things I tried were for example
>> $s= eval('' . "$val"); # (hoping to trigger the normal interpolation) or
>> $s=~ s/\\(.)/"\\$1"/eg;
>> but somehow i couldn't get it right ...
>>
>> Can anybody think of an elegant solution?
>
> eval "qq($val)";
>
> But note that the "\2" in your string (I don't know if it's a typo) will
> become
> a character with an octal value of 2, i.e. the ASCII STX control character.

I need to add that that solution relies on any parentheses in the string being in matching pairs. If you try

$val = ')(';

then it just won't work. If that's a problem, then you can use different
delimiters, which are guaranteed to be either escaped or not in the string at
all. The tilde is a useful one:

eval "qq~$val~";

or if you're relaly stuck, then any character at all will do, including control characters:

eval "qq\0$val\0";

works fine and delimits the string with null characters,

Cheers,

Rob


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to