Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Evals, quotes and backslashes problem

Reply
Thread Tools

Evals, quotes and backslashes problem

 
 
Paul Burton
Guest
Posts: n/a
 
      09-26-2003
I've got some code similar to this:
$a='$b="a_string"';
eval($a);
print $b;

For the above, this simply prints out "a_string".

Of course, I actually want something a little more interesting than
a_string. The output I actually want is the following string:
\$a_var

I thought I could achieve this by:
$a='$b="\\\$a_var"';
but this only produces the output:
\

I've managed to hack around this problem by generating my own special
string wherever I want a "\$" in the output, and using a string
substitution to put in the backslash:
$a='$b="\!\$a_var"'
eval($a);
$b =~ s/\!\$/\\\$/g;

which does the trick.

Is there a way of doing this without the substitution cludge, with some
clever combination of quotes and backslashes? I've tried a few things,
but nothing seems to work!

Cheers

Paul.

 
Reply With Quote
 
 
 
 
Stefan
Guest
Posts: n/a
 
      09-26-2003
Paul Burton wrote:
> I've got some code similar to this:
> $a='$b="a_string"';
> eval($a);
> print $b;
>
> For the above, this simply prints out "a_string".
>
> Of course, I actually want something a little more interesting than
> a_string. The output I actually want is the following string:
> \$a_var


I think you want:

$a = '$b="\\\\\\$a_var"';

Remember that because you have two sets of quotes, the string is
subjected to two levels of interpolation once it has been eval()ed.

After the line above, $a contains:

$b="\\\$a_var"

Then, when this value is eval()ed, $b becomes

\$a_var

See?


Stefan

 
Reply With Quote
 
 
 
 
Bill
Guest
Posts: n/a
 
      09-26-2003
Stefan <(E-Mail Removed)> wrote in message news:<bl1400$j3r$(E-Mail Removed)>...
> Paul Burton wrote:


> > $a='$b="a_string"';
> > eval($a);
> > print $b;


> I think you want:
>
> $a = '$b="\\\\\\$a_var"';
>
> After the line above, $a contains:
> $b="\\\$a_var"
>
> Then, when this value is eval()ed, $b becomes
> \$a_var


This is an excellent example of why doublequotes " " are often
inferior to single quotes ' ' unless variable substitution is really
needed.
 
Reply With Quote
 
Eric Amick
Guest
Posts: n/a
 
      09-27-2003
On Fri, 26 Sep 2003 11:21:51 +0100, Stefan <(E-Mail Removed)> wrote:

>Paul Burton wrote:
>> I've got some code similar to this:
>> $a='$b="a_string"';
>> eval($a);
>> print $b;
>>
>> For the above, this simply prints out "a_string".
>>
>> Of course, I actually want something a little more interesting than
>> a_string. The output I actually want is the following string:
>> \$a_var

>
>I think you want:
>
>$a = '$b="\\\\\\$a_var"';


Ugh. You'll find something like

$a = '$b = q(\$a_var)';

is less painful--and even readable.

--
Eric Amick
Columbia, MD
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP double quotes inside double quotes MSB Computer Support 11 10-21-2006 01:09 PM
Asp.NET Javascript string, want to pass '(single quotes' within '(single quotes) Chris ASP .Net 1 03-24-2006 09:03 PM
Replacing single quotes and backslashes in strings Phil Rhoades Ruby 3 12-20-2005 11:26 AM
Quotes/Double Quotes in Image Control Chris White ASP .Net 1 09-22-2004 06:22 AM
Multiline quotes - escaping quotes - et al Lawrence Tierney Java 3 12-24-2003 05:12 PM



Advertisments